-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.js
41 lines (38 loc) · 787 Bytes
/
day09.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
const fs = require('fs')
const infile = fs.readFileSync('input/day09', 'utf-8')
function day091(input){
input = input.split('\r\n').map(x=>parseInt(x))
let arr = []
let ans = -1
input.forEach((e,i)=>{
if(i<25) arr.push(e)
else{
let num = arr.map(b=>arr.map(c=>b+c==e?b+c:0)).flat().reduce((b,c)=>b+c)
if(num==0){
ans = e
}
arr.shift()
arr.push(e)
}
})
return ans
}
function day092(input){
input = input.split('\r\n').map(x=>parseInt(x))
let num = 776203571 //day091(input)
let sum=input[0]
let i = 0
let j = 0
while(sum!=num){
if(sum<num){
i++
sum+=input[i]
}else{
sum-=input[j]
j++
}
}
return Math.min.apply(null,input.slice(j,i))+Math.max.apply(null,input.slice(j,i))
}
console.log(day091(infile))
console.log(day092(infile))