mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-11-12 17:13:48 +00:00
init d9p2
This commit is contained in:
parent
72392d46be
commit
5cf420dc26
35
Day9/Part2.js
Normal file
35
Day9/Part2.js
Normal file
@ -0,0 +1,35 @@
|
||||
import { readFileSync } from "fs";
|
||||
const t0 = performance.now();
|
||||
|
||||
const input = readFileSync("input.txt")
|
||||
.toString()
|
||||
.split("\n")
|
||||
.filter((e) => e.length > 0);
|
||||
const histories = input.map((e) => e.split(" ").map(Number));
|
||||
const predictions = histories.map((e) => {
|
||||
let current = e;
|
||||
const sequences = [];
|
||||
while (!current.every((e) => e === 0)) {
|
||||
sequences.push(current);
|
||||
current = current.reduce((a, n, i) => {
|
||||
if (i === 0) return a;
|
||||
const d = n - current[i - 1];
|
||||
const t = a;
|
||||
t.push(d);
|
||||
return t;
|
||||
}, []);
|
||||
}
|
||||
const last = sequences.pop();
|
||||
const extrapolated = [[...last, last[0]]];
|
||||
for (const s of sequences.reverse()) {
|
||||
const current = s.pop();
|
||||
const last = extrapolated.pop().pop();
|
||||
extrapolated.push([...s, current + last]);
|
||||
}
|
||||
return extrapolated.pop().pop();
|
||||
});
|
||||
const sum = predictions.reduce((a, i) => a + i);
|
||||
console.log(sum);
|
||||
|
||||
const t1 = performance.now();
|
||||
console.log(`Runtime: ${t1 - t0}ms`);
|
Loading…
Reference in New Issue
Block a user