diff --git a/2023/day09/part2.ts b/2023/day09/part2.ts index 3692f70..8b51fa6 100644 --- a/2023/day09/part2.ts +++ b/2023/day09/part2.ts @@ -1,6 +1,32 @@ export function answer(input: string): number { - console.log(input); - return 42; + const seqs = input + .split("\n") + .map((l) => l.split(" ").map((x) => parseInt(x))); + + return seqs.reduce((sum, seq) => (sum += prevNum(seq)), 0); +} + +function prevNum(seq: number[]): number { + const seqs = [seq]; + const reduce = (s: number[]): number[] => { + let [prev, ...rest] = s; + return rest.map((x) => { + const next = x - prev; + prev = x; + return next; + }); + }; + + while (!seqs[0].every((x) => x === 0)) { + // console.log(reduce(seqs[0])); + seqs.unshift(reduce(seqs[0])); + } + for (const [i, s] of seqs.entries()) { + if (i === 0) continue; + const prevSeq = seqs[i - 1]; + s.unshift(s[0] - prevSeq[0]); + } + return seq[0]; } if (import.meta.main) { diff --git a/2023/day09/puzzle.md b/2023/day09/puzzle.md index d243946..62d5a42 100644 --- a/2023/day09/puzzle.md +++ b/2023/day09/puzzle.md @@ -97,8 +97,36 @@ If you find the next value for each history in this example and add them togethe Analyze your OASIS report and extrapolate the next value for each history. *What is the sum of these extrapolated values?* -To begin, [get your puzzle input](9/input). +Your puzzle answer was `1641934234`. + +The first half of this puzzle is complete! It provides one gold star: \* + +\--- Part Two --- +---------- + +Of course, it would be nice to have *even more history* included in your report. Surely it's safe to just *extrapolate backwards* as well, right? + +For each history, repeat the process of finding differences until the sequence of differences is entirely zero. Then, rather than adding a zero to the end and filling in the next values of each previous sequence, you should instead add a zero to the *beginning* of your sequence of zeroes, then fill in new *first* values for each previous sequence. + +In particular, here is what the third example history looks like when extrapolating back in time: + +``` +5 10 13 16 21 30 45 + 5 3 3 5 9 15 + -2 0 2 4 6 + 2 2 2 2 + 0 0 0 + +``` + +Adding the new values on the left side of each sequence from bottom to top eventually reveals the new left-most history value: `*5*`. + +Doing this for the remaining example data above results in previous values of `*-3*` for the first history and `*0*` for the second history. Adding all three new values together produces `*2*`. + +Analyze your OASIS report again, this time extrapolating the *previous* value for each history. *What is the sum of these extrapolated values?* Answer: -You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Mirage+Maintenance%22+%2D+Day+9+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F9&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file +Although it hasn't changed, you can still [get your puzzle input](9/input). + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Mirage+Maintenance%22+%2D+Day+9+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F9&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file diff --git a/2023/day09/test.ts b/2023/day09/test.ts index 94b4ef2..279db5c 100644 --- a/2023/day09/test.ts +++ b/2023/day09/test.ts @@ -11,7 +11,11 @@ Deno.test("part1", () => { assertEquals(p1.answer(examples), 114); }); -// Deno.test("part2", () => { -// const examples = ["abc", "def"].join("\n"); -// assertEquals(p2.answer(examples), 42); -// }); +Deno.test("part2", () => { + const examples = [ + "0 3 6 9 12 15", + "1 3 6 10 15 21", + "10 13 16 21 30 45", + ].join("\n"); + assertEquals(p2.answer(examples), 2); +});