-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (53 loc) · 1.7 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const fs = require('fs/promises');
const path = require('path');
/** @returns {Promise<number[][]>} */
const getParsedData = async (file = 'data.txt') => {
const data = await fs.readFile(path.resolve(__dirname, file), 'utf8');
return data.split(/\r?\n/).map(v => v.split(' ').map(n => parseInt(n)));
};
/** @param {number[]} lines */
const calcHistoryEnd = line => {
const newRows = [line];
let newLine = [];
for (let i = 0; i < newRows.length; i++) {
newLine = [];
const newLen = newRows[i].length;
for (let a = newLen - 1, b = newLen - 2; b >= 0; a--, b--) {
newLine.push(newRows[i][a] - newRows[i][b]);
}
newRows.push(newLine.reverse());
if (newLine.every(val => val === 0)) {
return newRows.reduce((total, row) => (total += row[row.length - 1]), 0);
}
}
};
/** @param {number[]} lines */
const calcHistoryStart = line => {
const newRows = [line];
let newLine = [];
for (let i = 0; i < newRows.length; i++) {
newLine = [];
const newLen = newRows[i].length;
for (let a = 0, b = 1; b < newLen; a++, b++) {
newLine.push(newRows[i][a] - newRows[i][b]);
}
newRows.push(newLine);
if (newLine.every(val => val === 0)) {
return newRows.reduce((total, row) => (total += row[0]), 0);
}
}
};
const day09 = async () => {
const lines = await getParsedData('data.txt');
const historyEnd = lines
.map(calcHistoryEnd)
.reduce((total, val) => (total += val), 0);
console.log('Total End:', historyEnd);
const historyStart = lines
.map(calcHistoryStart)
.reduce((total, val) => (total += val), 0);
console.log('Total Start:', historyStart);
};
//* Part #1 => 2101499000
//* Part #2 => 1089
module.exports = day09;