diff --git a/src/puzzles/day05.ts b/src/puzzles/day05.ts index ddf29d5..20a3079 100644 --- a/src/puzzles/day05.ts +++ b/src/puzzles/day05.ts @@ -55,24 +55,23 @@ const getStacks = (stacksRaw: string): Array => { 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, 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]); -} \ No newline at end of file + return { puzzle1: getSolution(stacks1, moves), puzzle2: getSolution(stacks2, moves, true) }; +} \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index e9acee4..c225e05 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,7 +7,7 @@ export interface Puzzles { export interface Solution { puzzle1?: number | string; - puzzle2?: number; + puzzle2?: number | string; } //Day 2 Specific @@ -44,4 +44,4 @@ export interface Day4Elf { export interface Day4ElfGroup { elf1: Day4Elf; elf2: Day4Elf; -} \ No newline at end of file +}