Part 1 day 12

This commit is contained in:
Daniel Flanagan 2020-12-11 23:29:49 -06:00
parent a85fb88c09
commit 83eafe403e
Signed by: lytedev
GPG Key ID: 5B2020A0F9921EF4
2 changed files with 23 additions and 5 deletions

View File

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

View File

@ -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]()