-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.ts
36 lines (25 loc) · 931 Bytes
/
day08.ts
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
import { readlines, parseProgram } from "./io"
import { Instruction, State, runInstruction, isComplete, runProgram } from "./util"
const isInfinite = (prog: Instruction[]): boolean => {
let state: State = { instructionPtr: 0, acc: 0 }
let seenPtrs: number[] = []
while (true) {
state = runInstruction(prog, state)
if (seenPtrs.includes(state.instructionPtr)) return true
if (isComplete(prog, state)) return false
seenPtrs.push(state.instructionPtr)
}
}
(async () => {
const raw = await readlines('day08.txt')
const prog = parseProgram(raw)
prog.forEach((instr, ptr) => {
const testProg = [...prog]
if (instr.op === 'jmp') testProg[ptr] = { ...instr, op: 'nop' }
else if (instr.op === 'nop') testProg[ptr] = { ...instr, op: 'jmp' }
else return
if (isInfinite(testProg)) return
const result = runProgram(testProg)
console.log(`${JSON.stringify(result)}`)
})
})()