diff --git a/2024/ts/day-11/code.ts b/2024/ts/day-11/code.ts index a345dce..f355110 100644 --- a/2024/ts/day-11/code.ts +++ b/2024/ts/day-11/code.ts @@ -1,6 +1,6 @@ // Solution for day 11 of advent of code 2024 -import { readInputLines, readInput } from "../system/aoc-helper"; +import { readInputLines, readInput, dlog } from "../system/aoc-helper"; import "../utils/array-helpers"; import { Puzzle } from "../model/puzzle"; @@ -11,16 +11,18 @@ const processInput = (day: number) => { }; const partOne = (input: number[], debug: boolean) => { - const stones = input.slice(); + let stones = input.slice(); const loopTarget = 25; for (let loop = 1; loop <= loopTarget; loop++) { let index = 0; + const nextStones = []; + while (index < stones.length) { const stone = stones[index]; if (stone === 0) { - stones[index] = 1; + nextStones.push(1); index += 1; continue; } @@ -28,24 +30,78 @@ const partOne = (input: number[], debug: boolean) => { if (stoneStr.length % 2 === 0) { const firstHalf = parseInt(stoneStr.slice(0, stoneStr.length / 2), 10); const secondHalf = parseInt(stoneStr.slice(stoneStr.length / 2), 10); - stones[index] = firstHalf; - stones.splice(index + 1, 0, secondHalf); - index += 2; + nextStones.push(firstHalf); + nextStones.push(secondHalf); + index += 1; continue; } - stones[index] = stones[index] * 2024; + nextStones.push(stones[index] * 2024); index += 1; } - //console.log(`Loop ${loop} - ${stones}`); + stones = nextStones; + dlog(`Loop ${loop} - ${stones}`); } return stones.length; }; +type LinkedList = { + value: number; + next: LinkedList | null; +} + +const toLinkedList = (input: number[]): LinkedList => { + let head: LinkedList = { value: input[0], next: null }; + let current = head; + for (let index = 1; index < input.length; index++) { + current.next = { value: input[index], next: null }; + current = current.next; + } + return head; +} + +const partOneLinkedList = (input: number[], debug: boolean) => { + let stones = input.slice(); + + const head = toLinkedList(stones); + let count = stones.length; + + const loopTarget = 25; + + for (let loop = 1; loop <= loopTarget; loop++) { + let current: LinkedList | null = head; + + while (current !== null) { + if (current.value === 0) { + current.value = 1; + current = current.next; + continue; + } + const stoneStr = current.value.toString(); + if (stoneStr.length % 2 === 0) { + const firstHalf = parseInt(stoneStr.slice(0, stoneStr.length / 2), 10); + const secondHalf = parseInt(stoneStr.slice(stoneStr.length / 2), 10); + current.value = firstHalf; + const newStone = { value: secondHalf, next: current.next }; + current.next = newStone; + current = newStone.next; + count += 1; + continue; + } + + current.value = current.value * 2024; + current = current.next; + } + + dlog(`Loop ${loop} - ${count}`); + } + return count; +}; + const partTwo = (input: number[], debug: boolean) => { let stones = input.slice(); - const loopTarget = 75; + const loopTarget = 10; for (let loop = 1; loop <= loopTarget; loop++) { const newStones = []; diff --git a/2024/ts/day-11/input.txt b/2024/ts/day-11/input.txt index 528f9d5..889410e 100644 --- a/2024/ts/day-11/input.txt +++ b/2024/ts/day-11/input.txt @@ -1 +1 @@ -125 17 \ No newline at end of file +27 10647 103 9 0 5524 4594227 902936 \ No newline at end of file