Skip to content

Commit

Permalink
🎅🏻Adds Puzzle 2 to Day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmetzgar committed Dec 5, 2022
1 parent bdd0d27 commit 9edea6d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions src/puzzles/day05.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,23 @@ const getStacks = (stacksRaw: string): Array<string[]> => {
return columns;
}

export default (dataSet: string): Solution => {

const data = dataSet.split('\n\n');
const stacks = getStacks(data[0]);
const moves = getMoves(data[1]);

const getSolution = (stacks: Array<string[]>, moves: Move[], puzzle2 = false): string => {
moves.forEach((move: Move) => {
const items = stacks[move.from].splice(stacks[move.from].length - move.items, move.items);
//reverse the items so they are in the correct order when added to the new stack
items.reverse();
if (puzzle2 !== true) items.reverse();
//add the items to the new stack
stacks[move.to].push(...items);
});

console.log(stacks);
// Get last item of each stack and convert to string
const result = stacks.map((stack: string[]) => stack[stack.length - 1]).join('');
return stacks.map((stack: string[]) => stack[stack.length - 1] || ' ').join('');
}

return { puzzle1: result };
export default (dataSet: string): Solution => {
const data = dataSet.split('\n\n');
const stacks1 = getStacks(data[0]);
const stacks2 = getStacks(data[0]);
const moves = getMoves(data[1]);

}
return { puzzle1: getSolution(stacks1, moves), puzzle2: getSolution(stacks2, moves, true) };
}
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface Puzzles {

export interface Solution {
puzzle1?: number | string;
puzzle2?: number;
puzzle2?: number | string;
}

//Day 2 Specific
Expand Down Expand Up @@ -44,4 +44,4 @@ export interface Day4Elf {
export interface Day4ElfGroup {
elf1: Day4Elf;
elf2: Day4Elf;
}
}

0 comments on commit 9edea6d

Please sign in to comment.