-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_09.mjs
31 lines (26 loc) · 1.14 KB
/
day_09.mjs
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
import * as fs from 'node:fs';
let inputData = fs
.readFileSync('input.txt', 'utf-8')
.trim()
.split('\n')
.map(line => line.split(' ').map(n => parseInt(n)));
const toSum = (acc, cur) => (acc || 0) + cur;
const breakDownSequence = (seq) => {
const steps = [seq];
while (!steps[steps.length - 1].every(n => n === 0)) {
steps.push(steps[steps.length - 1].map((n, i, arr) => arr[i + 1] - n).slice(0, -1));
}
return steps;
};
const continueSequence = (seq, direction) => {
const structure = breakDownSequence(seq).toReversed();
const position = { next: -1, prev: 0 };
const _continue = {
next: (element, i) => {structure[i + 1] = [...structure, element.at(-1) + structure[i + 1].at(-1)];},
prev: (element, i) => {structure[i + 1] = [structure[i + 1].at(0) - element.at(0), ...structure[i + 1]];},
};
for (let i = 0; i < structure.length - 1; i++) _continue[direction](structure[i], i);
return structure.at(-1).at(position[direction]);
};
console.log(`Part 1: ${inputData.map(seq => continueSequence(seq, 'next')).reduce(toSum)}`);
console.log(`Part 2: ${inputData.map(seq => continueSequence(seq, 'prev')).reduce(toSum)}`);