Add day 8 part 1
This commit is contained in:
parent
32f530c004
commit
85ef4400a9
|
@ -9,4 +9,4 @@ proc solve_for_day(n: int) {.used.} =
|
|||
|
||||
when isMainModule:
|
||||
# solve_all()
|
||||
solve_for_day(7)
|
||||
solve_for_day(8)
|
||||
|
|
27
2020/src/day8.nim
Normal file
27
2020/src/day8.nim
Normal file
|
@ -0,0 +1,27 @@
|
|||
import streams, strutils, sequtils, sets
|
||||
|
||||
type
|
||||
InstructionType = enum nop acc jmp
|
||||
Instruction = ref object of RootObj
|
||||
instruction: InstructionType
|
||||
arg: int
|
||||
|
||||
proc instructions(s: Stream): seq[Instruction] =
|
||||
toSeq(s.lines).mapIt(it.split).mapIt(Instruction(instruction: parseEnum[InstructionType](it[0]), arg: parseInt(it[1].replace("+"))))
|
||||
|
||||
proc part1*(s: Stream): int =
|
||||
var hasRun = initHashSet[int]()
|
||||
var p = 0
|
||||
let code = s.instructions
|
||||
while true:
|
||||
if hasRun.contains p: return result
|
||||
hasRun.incl p
|
||||
let i = code[p]
|
||||
case i.instruction:
|
||||
of acc: inc result, i.arg
|
||||
of jmp: p += i.arg; continue
|
||||
else: discard
|
||||
inc p
|
||||
|
||||
proc part2*(s: Stream): int =
|
||||
0
|
Loading…
Reference in a new issue