This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.nim
92 lines (80 loc) · 2.24 KB
/
day13.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import algorithm, strutils
type
Cart = ref object
x, y, dx, dy, turn: int
crashed: bool
proc newCart(x, y, dx, dy: int): Cart =
result = new(Cart)
result.x = x
result.y = y
result.dx = dx
result.dy = dy
proc cmpCarts(x, y: Cart): int =
if x.y == y.y:
x.x - y.x
else:
x.y - y.y
proc cartsAt(carts: seq[Cart], x,y: int): seq[Cart] =
for cart in carts:
if cart.x == x and cart.y == y and not cart.crashed:
result.add(cart)
proc move(cart: Cart, grid: seq[string]) =
cart.x += cart.dx
cart.y += cart.dy
var
dx = cart.dx
dy = cart.dy
let cell = grid[cart.y][cart.x]
if cell == '/':
cart.dx = -dy
cart.dy = -dx
if cell == '\\':
cart.dx = dy
cart.dy = dx
elif cell == '+':
if cart.turn == 0:
cart.dx = dy
cart.dy = -dx
elif cart.turn == 2:
cart.dx = -dy
cart.dy = dx
cart.turn = (cart.turn + 1) mod 3
var
lines = readFile("data/13.txt").splitlines
grid: seq[string]
carts: seq[Cart]
for i, line in lines:
var gridLine = ""
for j, c in line:
case c:
of '>':
carts.add(newCart(j, i, 1, 0))
gridLine &= '-'
of '<':
carts.add(newCart(j, i, -1, 0))
gridLine &= '-'
of 'v':
carts.add(newCart(j, i, 0, 1))
gridLine &= '|'
of '^':
carts.add(newCart(j, i, 0, -1))
gridLine &= '|'
else:
gridLine &= c
grid.add(gridLine)
var uncrashed = carts.len
while uncrashed > 1:
carts.sort(cmpCarts)
for cart in carts:
if not cart.crashed:
cart.move(grid)
var dupes = carts.cartsAt(cart.x, cart.y)
if dupes.len > 1:
if uncrashed == carts.len:
echo cart.x, ",", cart.y
for crash in dupes:
crash.crashed = true
uncrashed.dec
for cart in carts:
if not cart.crashed:
echo cart.x, ",", cart.y