From dd6eb3ae3dd235d5ad953ccf6acb044496f51a9f Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 9 Dec 2023 22:19:40 +0100 Subject: [PATCH] split history into difference sequences --- Day9/Part1.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Day9/Part1.js b/Day9/Part1.js index 3a620a8..f65271d 100644 --- a/Day9/Part1.js +++ b/Day9/Part1.js @@ -1,8 +1,24 @@ import { readFileSync } from "fs"; const t0 = performance.now(); -const input = readFileSync("input.txt").toString(); -console.log(input); +const input = readFileSync("input.txt") + .toString() + .split("\n") + .filter((e) => e.length > 0); +const histories = input.map((e) => e.split(" ").map(Number)); +const sequences = []; +let current = histories[0]; +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; + }, []); +} +console.log(sequences); const t1 = performance.now(); console.log(`Runtime: ${t1 - t0}ms`);