extrapolate to prediction, then sum them

This commit is contained in:
Baipyrus 2023-12-09 22:22:32 +01:00
parent dd6eb3ae3d
commit 72392d46be

View File

@ -6,19 +6,30 @@ const input = readFileSync("input.txt")
.split("\n") .split("\n")
.filter((e) => e.length > 0); .filter((e) => e.length > 0);
const histories = input.map((e) => e.split(" ").map(Number)); const histories = input.map((e) => e.split(" ").map(Number));
const sequences = []; const predictions = histories.map((e) => {
let current = histories[0]; let current = e;
while (!current.every((e) => e === 0)) { const sequences = [];
sequences.push(current); while (!current.every((e) => e === 0)) {
current = current.reduce((a, n, i) => { sequences.push(current);
if (i === 0) return a; current = current.reduce((a, n, i) => {
const d = n - current[i - 1]; if (i === 0) return a;
const t = a; const d = n - current[i - 1];
t.push(d); const t = a;
return t; t.push(d);
}, []); return t;
} }, []);
console.log(sequences); }
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(); const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`); console.log(`Runtime: ${t1 - t0}ms`);