-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.js
34 lines (30 loc) · 781 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
const {readFile, sumAll} = require('./helpers/utils')
const input = readFile('input/10-1.txt').split('\n').slice(0, -1)
// ---------------------------------------------
let x = 1
let c = 0
let strengths = []
let screen = []
const tick = () => {
let pixel = c%40-1
const draw = Math.abs(x-pixel) <= 1
screen.push({c: c-1, s: (draw)?'#':'.'})
strengths.push({c, s: c * x})
}
input.forEach(line => {
[cmd, a] = line.split(' ')
c++
tick()
if (cmd == 'addx') {
c++
tick()
x += +a
}
})
let res = strengths.filter(x => x.c%40 == 20)
console.log('Part 1', res.reduce((acc,x) => acc+x.s, 0))
// console.log(screen)
let res2 = screen.map(x => {
return ((x.c)%40==0 ? '\n' : '') + x.s
}).join('')
console.log('Part 2', res2)