-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.js
40 lines (32 loc) · 1.18 KB
/
day09.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const readFrom = require("./read-input");
const args = process.argv;
const input = readFrom(args);
if (!input) return;
const [partOneResult, partTwoResult] = input.split("\n").reduce(
([future, past], reportLine) => {
const report = reportLine.split(" ").map((v) => parseInt(v));
const getDifferences = (report) => {
return report.slice(1).reduce((diffs, value, i) => {
return diffs.concat(value - report[i]);
}, []);
};
const firstItems = [];
const lastItems = [];
let currentDiffs = report;
while (!getDifferences(currentDiffs).every((v) => v === 0)) {
currentDiffs = getDifferences(currentDiffs);
firstItems.push(currentDiffs[0]);
lastItems.push(currentDiffs[currentDiffs.length - 1]);
}
const newFuture =
future +
lastItems.reduce((sum, val) => sum + val) +
report[report.length - 1];
const newPast =
past + report[0] - firstItems.reduceRight((sum, val) => val - sum, 0);
return [newFuture, newPast];
},
[0, 0]
);
console.log(partOneResult);
console.log(partTwoResult);