Add day 8 part 1

This commit is contained in:
Daniel Flanagan 2020-12-07 23:17:17 -06:00
parent 32f530c004
commit 85ef4400a9
Signed by: lytedev
GPG Key ID: 5B2020A0F9921EF4
2 changed files with 28 additions and 1 deletions

View File

@ -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
View 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