-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathday11.nim
More file actions
30 lines (22 loc) · 703 Bytes
/
day11.nim
File metadata and controls
30 lines (22 loc) · 703 Bytes
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
import strutils
const instructions = readFile("./inputs/11.txt").split(",")
type Point = tuple[p: int, q: int]
func toAxial(direction: string): Point =
case direction
of "n": ( 0, -1)
of "nw": (-1, 0)
of "sw": (-1, 1)
of "s": ( 0, 1)
of "se": ( 1, 0)
else: ( 1, -1)
func `+`(a, b: Point): Point = (a.p + b.p, a.q + b.q)
func `+=`(a: var Point, b: Point) = a = a + b
func distanceToOrigin(a: Point): int = max([abs(a.p), abs(a.q), abs(a.p + a.q)])
var
position: Point
distances: seq[int] = @[]
for direction in instructions:
position += direction.toAxial()
distances.add(position.distanceToOrigin())
echo position.distanceToOrigin()
echo max(distances)