-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.ts
More file actions
54 lines (47 loc) · 1.32 KB
/
solve.ts
File metadata and controls
54 lines (47 loc) · 1.32 KB
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
import { readFileSync } from 'node:fs'
const sample = readFileSync('sample.txt').toString('utf-8').trimEnd()
const real = readFileSync('real.txt').toString('utf-8').trimEnd()
console.log('sample answer A:', solveA(sample))
console.log('real answer A:', solveA(real))
console.log('sample answer B:', solveB(sample))
console.log('real answer B:', solveB(real))
function solveA(input: string) {
let cycles = [1]
input.split('\n').map((l) => {
const x = cycles[cycles.length - 1]!
const p = l.split(' ')
switch (p[0]) {
case 'noop':
cycles.push(x)
break
case 'addx':
cycles.push(x, x + Number(p[1]))
break
}
})
const signal = (c: number) => c * cycles[c - 1]!
return [20, 60, 100, 140, 180, 220].map((c) => signal(c)).reduce((a, b) => a + b, 0)
}
function solveB(input: string) {
const out: string[] = []
let x = 1
let cycles = 0
input.split('\n').map((l) => {
const render = () => {
out.push([x - 1, x, x + 1].includes(cycles % 40) ? '#' : '.')
cycles++
}
const p = l.split(' ')
switch (p[0]) {
case 'noop':
render()
break
case 'addx':
render()
render()
x += Number(p[1])
break
}
})
return [0, 40, 80, 120, 160, 200].map((v) => '\n' + out.slice(v, v + 40).join('')).join('')
}