-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday12.nim
37 lines (32 loc) · 1005 Bytes
/
day12.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import aoc, strutils
type
Instruction = tuple[action: char, value: int]
Instructions = seq[Instruction]
proc parse(path: string): Instructions =
for line in path.lines:
result.add (line[0], parseInt(line[1..^1]))
func solve(instructions: Instructions, part: int): int =
var dir, pos = new Point
dir[] = if part == Part1: (1, 0) else: (10, -1)
pos[] = (0, 0)
var movable = if part == Part1: pos else: dir
for instr in instructions:
case instr.action
of 'E': movable.x += instr.value
of 'W': movable.x -= instr.value
of 'S': movable.y += instr.value
of 'N': movable.y -= instr.value
of 'F':
pos.x += dir.x * instr.value
pos.y += dir.y * instr.value
of 'L':
for _ in 1 .. (instr.value div 90):
dir[] = (dir.y, -dir.x)
of 'R':
for _ in 1 .. (instr.value div 90):
dir[] = (-dir.y, dir.x)
else: discard
result = manhattan pos[]
let input = parse "inputs/12.txt"
echo input.solve Part1
echo input.solve Part2