-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc10.js
42 lines (33 loc) · 1.17 KB
/
aoc10.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
const { sum, range, zip } = require('lodash');
const log = (arg) => console.log(arg) && arg;
require('fs').readFile('./aoc10.txt', 'utf8', (_, content) => {
const data = content
.split('\n')
.map(Number)
main(data);
main2(data);
});
function main(data) {
const sorted = [0, ...data.slice().sort((a, b) => a - b), 150 /* max + 3 */];
const diffs = zip(sorted.slice(0, -1), sorted.slice(1)).map(([a, b]) => b - a);
const oneJoltCount = diffs.filter(n => n === 1).length;
const threeJoltCount = diffs.filter(n => n === 3).length;
log(oneJoltCount * threeJoltCount);
}
const cache = new Map();
function computePaths(data) {
if(cache.has(data[0])) return cache.get(data[0]);
if (data.length === 1) return 1;
const nexts = data.slice(1, 4);
const childrenPathsCount = nexts
.filter(x => x - data[0] <= 3)
.map((_, i) => computePaths(data.slice(i + 1)));
const result = sum(childrenPathsCount);
cache.set(data[0], result);
return result;
}
function main2(data) {
const sorted = [0, ...data.slice().sort((a, b) => a - b)];
const result = computePaths(sorted);
log(result);
}