-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.js
46 lines (35 loc) · 963 Bytes
/
10.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
const util = require('./util.js')
const TODAY = 10
function main() {
const ops = util.getInputFromFile(TODAY)
.split('\n').map(e => parseInt(e))
console.log("Part 1: " + part1(ops))
console.log("Part 2: " + part2(ops))
}
function part1(ops) {
ops = ops.sort((a,b)=>a-b)
let i = 0, e = 0
for (let a = 1; a<ops.length;a++) {
let o = ops[a] - ops[a-1]
if(o === 1) {
i++
} else if (o === 3) {
e++
}
}
return (i+1)*(e+1)
}
// custom offset tribonacci(0)=1 tribonacci(1)=1
let tribonacci_cust = (n) => n < 0
? 0
: n < 2
? 1
: tribonacci_cust(n-1) + tribonacci_cust(n-2) + tribonacci_cust(n-3)
function part2(ops) {
return ops.sort((a,b)=>a-b)
.map((elem, index) => elem - (ops[index-1] || 0))
.join('')
.split('3')
.reduce((agg, el) => agg * tribonacci_cust(el.length), 1)
}
main();