this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

part 2! thank god that one was quick

+66 -8
+28 -2
2023/day09/part2.ts
··· 1 1 export function answer(input: string): number { 2 - console.log(input); 3 - return 42; 2 + const seqs = input 3 + .split("\n") 4 + .map((l) => l.split(" ").map((x) => parseInt(x))); 5 + 6 + return seqs.reduce((sum, seq) => (sum += prevNum(seq)), 0); 7 + } 8 + 9 + function prevNum(seq: number[]): number { 10 + const seqs = [seq]; 11 + const reduce = (s: number[]): number[] => { 12 + let [prev, ...rest] = s; 13 + return rest.map((x) => { 14 + const next = x - prev; 15 + prev = x; 16 + return next; 17 + }); 18 + }; 19 + 20 + while (!seqs[0].every((x) => x === 0)) { 21 + // console.log(reduce(seqs[0])); 22 + seqs.unshift(reduce(seqs[0])); 23 + } 24 + for (const [i, s] of seqs.entries()) { 25 + if (i === 0) continue; 26 + const prevSeq = seqs[i - 1]; 27 + s.unshift(s[0] - prevSeq[0]); 28 + } 29 + return seq[0]; 4 30 } 5 31 6 32 if (import.meta.main) {
+30 -2
2023/day09/puzzle.md
··· 97 97 98 98 Analyze your OASIS report and extrapolate the next value for each history. *What is the sum of these extrapolated values?* 99 99 100 - To begin, [get your puzzle input](9/input). 100 + Your puzzle answer was `1641934234`. 101 + 102 + The first half of this puzzle is complete! It provides one gold star: \* 103 + 104 + \--- Part Two --- 105 + ---------- 106 + 107 + 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? 108 + 109 + 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. 110 + 111 + In particular, here is what the third example history looks like when extrapolating back in time: 112 + 113 + ``` 114 + 5 10 13 16 21 30 45 115 + 5 3 3 5 9 15 116 + -2 0 2 4 6 117 + 2 2 2 2 118 + 0 0 0 119 + 120 + ``` 121 + 122 + Adding the new values on the left side of each sequence from bottom to top eventually reveals the new left-most history value: `*5*`. 123 + 124 + 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*`. 125 + 126 + Analyze your OASIS report again, this time extrapolating the *previous* value for each history. *What is the sum of these extrapolated values?* 101 127 102 128 Answer: 103 129 104 - 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. 130 + Although it hasn't changed, you can still [get your puzzle input](9/input). 131 + 132 + 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.
+8 -4
2023/day09/test.ts
··· 11 11 assertEquals(p1.answer(examples), 114); 12 12 }); 13 13 14 - // Deno.test("part2", () => { 15 - // const examples = ["abc", "def"].join("\n"); 16 - // assertEquals(p2.answer(examples), 42); 17 - // }); 14 + Deno.test("part2", () => { 15 + const examples = [ 16 + "0 3 6 9 12 15", 17 + "1 3 6 10 15 21", 18 + "10 13 16 21 30 45", 19 + ].join("\n"); 20 + assertEquals(p2.answer(examples), 2); 21 + });