Pausing work on day8p2
This commit is contained in:
parent
85ef4400a9
commit
b3bf458d3a
|
@ -2,26 +2,55 @@ import streams, strutils, sequtils, sets
|
||||||
|
|
||||||
type
|
type
|
||||||
InstructionType = enum nop acc jmp
|
InstructionType = enum nop acc jmp
|
||||||
Instruction = ref object of RootObj
|
Instruction = object
|
||||||
instruction: InstructionType
|
instruction: InstructionType
|
||||||
arg: int
|
arg: int
|
||||||
|
HandheldState = object
|
||||||
|
bootcode: seq[Instruction]
|
||||||
|
acc: int
|
||||||
|
pointer: int
|
||||||
|
running: bool
|
||||||
|
|
||||||
|
proc newHandheld(bootcode: seq[Instruction], acc = 0, pointer = 0, running = true): HandheldState =
|
||||||
|
HandheldState(bootcode: bootcode, acc: acc, pointer: pointer, running: running)
|
||||||
|
|
||||||
proc instructions(s: Stream): seq[Instruction] =
|
proc instructions(s: Stream): seq[Instruction] =
|
||||||
toSeq(s.lines).mapIt(it.split).mapIt(Instruction(instruction: parseEnum[InstructionType](it[0]), arg: parseInt(it[1].replace("+"))))
|
toSeq(s.lines).mapIt(it.split).mapIt(Instruction(instruction: parseEnum[InstructionType](it[0]), arg: parseInt(it[1].replace("+"))))
|
||||||
|
|
||||||
|
proc stepHandheld(state: var HandheldState) =
|
||||||
|
if state.pointer >= state.bootcode.len:
|
||||||
|
echo "Reached end of instructions"
|
||||||
|
state.running = false
|
||||||
|
let i = state.bootcode[state.pointer]
|
||||||
|
case i.instruction:
|
||||||
|
of acc: inc state.acc, i.arg
|
||||||
|
of jmp: inc state.pointer, i.arg; return
|
||||||
|
else: discard
|
||||||
|
inc state.pointer
|
||||||
|
|
||||||
proc part1*(s: Stream): int =
|
proc part1*(s: Stream): int =
|
||||||
var hasRun = initHashSet[int]()
|
var hasRun = initHashSet[int]()
|
||||||
var p = 0
|
var handheld = newHandheld(s.instructions)
|
||||||
let code = s.instructions
|
while handheld.running:
|
||||||
while true:
|
stepHandheld handheld
|
||||||
if hasRun.contains p: return result
|
if hasRun.contains handheld.pointer: return handheld.acc
|
||||||
hasRun.incl p
|
hasRun.incl handheld.pointer
|
||||||
let i = code[p]
|
|
||||||
|
proc correctingHandheld(state: var HandheldState) =
|
||||||
|
if state.pointer >= state.bootcode.len:
|
||||||
|
echo "Reached end of instructions"
|
||||||
|
state.running = false
|
||||||
|
let i = state.bootcode[state.pointer]
|
||||||
case i.instruction:
|
case i.instruction:
|
||||||
of acc: inc result, i.arg
|
of acc: inc state.acc, i.arg
|
||||||
of jmp: p += i.arg; continue
|
of jmp: inc state.pointer, i.arg; return
|
||||||
else: discard
|
else: discard
|
||||||
inc p
|
inc state.pointer
|
||||||
|
|
||||||
proc part2*(s: Stream): int =
|
proc part2*(s: Stream): int =
|
||||||
0
|
var hasRun = initHashSet[int]()
|
||||||
|
var handheld = newHandheld(s.instructions)
|
||||||
|
while handheld.running:
|
||||||
|
stepHandheld handheld
|
||||||
|
if hasRun.contains handheld.pointer: return handheld.acc
|
||||||
|
hasRun.incl handheld.pointer
|
||||||
|
|
Loading…
Reference in a new issue