mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-27 04:21:45 +00:00
extrapolate to prediction, then sum them
This commit is contained in:
parent
dd6eb3ae3d
commit
72392d46be
@ -6,8 +6,9 @@ const input = readFileSync("input.txt")
|
||||
.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 = [];
|
||||
let current = histories[0];
|
||||
while (!current.every((e) => e === 0)) {
|
||||
sequences.push(current);
|
||||
current = current.reduce((a, n, i) => {
|
||||
@ -18,7 +19,17 @@ while (!current.every((e) => e === 0)) {
|
||||
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();
|
||||
console.log(`Runtime: ${t1 - t0}ms`);
|
||||
|
Loading…
Reference in New Issue
Block a user