diff --git a/2020/src/aoc2020.nim b/2020/src/aoc2020.nim index 73e4776..9908b30 100644 --- a/2020/src/aoc2020.nim +++ b/2020/src/aoc2020.nim @@ -9,4 +9,4 @@ proc solve_for_day(n: int) {.used.} = when isMainModule: # solve_all() - solve_for_day(7) + solve_for_day(8) diff --git a/2020/src/day8.nim b/2020/src/day8.nim new file mode 100644 index 0000000..3bc5e43 --- /dev/null +++ b/2020/src/day8.nim @@ -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