-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_08.js
81 lines (68 loc) · 1.46 KB
/
day_08.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const rawInput = require('./data/day_08').split('\n')
const examp = `nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6`
.split('\n')
const assert = require('assert-fine')
const real = 1
let input = real ? rawInput : examp
const program = []
for (const str of input) {
const r = /^(\w+)\s(.)(\d+)/.exec(str)
assert(r, 'BAD', str)
let v = r[3] * ((r[2] === '-') ? -1 : 1)
program.push([r[1], v])
}
const exec = (prog) => {
const nums = new Set(), top = prog.length
let acc = 0, i = 0, last, loop = false, high = 0, jmpM = 0, nopM = 0
while (prog[i] !== undefined) {
if ((loop = nums.has(i))) break
nums.add(last = i)
high = Math.max(i, high)
const [cmd, val] = prog[i]
switch (cmd) {
case 'acc':
acc += val
break
case'nop':
// if ((val + i) >= top)
nopM = Math.max(i, nopM)
break
case'jmp':
jmpM = Math.max(i, jmpM)
i += val
continue
default:
assert(0, 'Unknown', cmd)
}
i += 1
}
return { acc, i, last, high, loop, jmpM, nopM }
}
let res = exec(program)
console.log('Q1', res.acc)
for (let i = program.length; --i >= 0;) {
const cmd = program[i][0]
if (cmd === 'jmp') {
program[i][0] = 'nop'
} else if (cmd === 'nop') {
program[i][0] = 'jmp'
} else {
continue
}
const res = exec(program)
if (!res.loop) {
console.log(res)
break
}
program[i][0] = cmd
}
console.log('all')