advent-of-code/2020/src/day12.nim

58 lines
1.9 KiB
Nim
Raw Normal View History

2020-12-13 00:04:37 -06:00
import streams, strutils, tables
2020-12-11 23:02:43 -06:00
type Dir = enum north, east, south, west
proc rotate(d: Dir, n = 0): Dir = Dir((ord(d) + n) %% 4)
proc left(d: Dir, n = 1): Dir = d.rotate(n)
proc right(d: Dir, n = 1): Dir = d.rotate(-n)
2020-12-11 23:29:49 -06:00
const dirMap = {north: (0, -1), east: (1, 0), south: (0, 1), west: (-1, 0)}.toTable
proc move(pos: (int, int), nvel: (int, int), mag = 1): (int, int) =
2020-12-11 23:29:49 -06:00
((pos[0] + (nvel[0] * mag)), (pos[1] + (nvel[1] * mag)))
2020-12-11 23:02:43 -06:00
proc part1*(s: Stream): int =
2020-12-11 23:29:49 -06:00
var d = east
var p = (0, 0)
2020-12-11 23:02:43 -06:00
for l in s.lines:
2020-12-11 23:29:49 -06:00
let arg = l[1..^1].parseInt
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 `-`(a: (int, int), b: (int, int)): (int, int) = (a[0] - b[0], a[1] - b[1])
proc `+`(a: (int, int), b: (int, int)): (int, int) = (a[0] + b[0], a[1] + b[1])
proc left(p: (int, int), n = 1): (int, int) =
result = (p[1],-p[0])
if n > 1: result = left(result, n - 1)
proc right(p: (int, int), n = 1): (int, int) =
result = (-p[1],p[0])
if n > 1: result = right(result, n - 1)
2020-12-11 23:02:43 -06:00
proc part2*(s: Stream): int =
var d = east
var p = (0, 0)
var wp = (10, -1)
for l in s.lines:
let arg = l[1..^1].parseInt
case l[0]:
of 'N': wp = wp.move(dirMap[north], arg)
of 'S': wp = wp.move(dirMap[south], arg)
of 'E': wp = wp.move(dirMap[east], arg)
of 'W': wp = wp.move(dirMap[west], arg)
of 'L': wp = wp.left(arg div 90)
of 'R': wp = wp.right(arg div 90)
else: p = p.move(wp, arg)
p[0].abs + p[1].abs
doAssert "F10\nN3\nF7\nR90\nF11".newStringStream.part2 == 286
doAssert left((9, 8), 2) == (-9, -8)
doAssert left((9, 1)) == (1, -9)
doAssert right((1, 9)) == (-9, 1)