From 85ef4400a937f483f5a755ffb11f9cae18abce0a Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 7 Dec 2020 23:17:17 -0600 Subject: [PATCH] Add day 8 part 1 --- 2020/src/aoc2020.nim | 2 +- 2020/src/day8.nim | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 2020/src/day8.nim 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