-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9_2.js
43 lines (38 loc) · 981 Bytes
/
day9_2.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
41
42
43
const { parse } = require('path');
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream('input.txt'),
});
let sum = 0;
function findHistory(arr) {
const history = [];
history.push(arr);
let current = arr;
while (!current.every((value) => value === 0)) {
const next = [];
for (let i = 1; i < current.length; i++) {
next.push(current[i] - current[i - 1]);
}
history.push(next);
current = next;
}
return history;
}
function findFirstInSequence(history) {
let prev = 0;
for (let i = history.length - 2; i >= 0; i--) {
prev = history[i][0] - prev;
}
return prev;
}
lineReader.on('line', function (line) {
const numbers = line
.split(' ')
.map((value) => parseInt(value))
.filter((value) => !isNaN(value));
const history = findHistory(numbers);
const first = findFirstInSequence(history);
sum += first;
});
lineReader.on('close', function () {
console.log(sum);
});