diff --git a/2020/src/day12.nim b/2020/src/day12.nim index da5606d..5e5f346 100644 --- a/2020/src/day12.nim +++ b/2020/src/day12.nim @@ -1,13 +1,31 @@ -import streams, sequtils +import streams, sequtils, strutils, tables type Dir = enum north, east, south, west -proc left( +proc left(d: Dir, n = 1): Dir = Dir((ord(d) - n) %% 4) +proc right(d: Dir, n = 1): Dir = Dir((ord(d) + n) %% 4) + +const dirMap = {north: (0, -1), east: (1, 0), south: (0, 1), west: (-1, 0)}.toTable + +proc move(pos: (int, int), nvel: (int, int), mag: int): (int, int) = + ((pos[0] + (nvel[0] * mag)), (pos[1] + (nvel[1] * mag))) proc part1*(s: Stream): int = + var d = east + var p = (0, 0) for l in s.lines: - echo l - 9 + let arg = l[1..^1].parseInt + echo arg + case l[0]: + of 'N': p = p.move(dirMap[north], arg) + of 'S': p = p.move(dirMap[south], arg) + of 'E': p = p.move(dirMap[east], arg) + of 'W': p = p.move(dirMap[west], arg) + of 'L': d = d.left(arg div 90) + of 'R': d = d.right(arg div 90) + else: p = p.move(dirMap[d], arg) + p[0].abs + p[1].abs + proc part2*(s: Stream): int = 9 diff --git a/2020/src/day8.nim b/2020/src/day8.nim index da3eb4b..ae5a31b 100644 --- a/2020/src/day8.nim +++ b/2020/src/day8.nim @@ -54,7 +54,7 @@ proc flipped(h: var HandheldState): HandheldState = proc altHandheld(h: var HandheldState): Option[HandheldState] = case h.bootcode[h.pointer].instruction: of acc: return none(HandheldState) - of jmp, nop: some(h.flipped) + of jmp, nop: return some(h.flipped) proc part2*(s: Stream): int = var alts = initTable[int, HandheldState]()