-
Notifications
You must be signed in to change notification settings - Fork 1
/
13.q
61 lines (56 loc) · 1.29 KB
/
13.q
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
/--- Day 13: Mine Cart Madness ---
/ coordinates of carts
carts:raze { y,'where any x=/:"^v<>" }'[r;til count r:read0 `:input/13.txt]
/ current direction of carts
carts:carts,'enlist each t:.[r;] each carts
/ set next turn to left
carts:carts,'"l"
/ replace carts with straight track
.[`r;;:;].'flip(carts[;0 1];"|-"t in\:"<>");
dir:{ (-1 0;0 1;1 0;0 -1) "^>v<"?x }
turn:{ (("<>";"><";"v^";"^v")"^v><"?x)y="/" }
crossroad:{ ((("<^>";">v<";"^>v";"v<^")"^v><"?x)"lsr"?y;"srl""lsr"?y) }
crashed:0b
go:{[x]
/ already crashed
if[x in d;:()];
/ pick out cart
c:carts x;
/ position of moved cart
p:.[r;] n:c[0 1] + dir c 2;
/ crash
if[n in carts[;0 1];
/ part 1
if[not crashed;
-1@","sv string reverse n;
/ 58,93
crashed::1b
];
/ return crasher and crashee
:d,:x,first where carts[;0 1]~\:n
];
/ turn cart
if[p in "/\\";
c[2]:turn[c 2;p]
];
/ crossroad
if[p="+";
c[2 3]:crossroad[c 2;c 3];
];
/ update new cart position
c[0 1]:n;
/ update cart in carts
carts[x]:c;
/ nothing to return
:()
};
while[not 1=count carts;
/ reset drop list
d:();
/ process each cart
res:raze go each iasc carts[;0 1];
/ drop crashed carts
carts:carts (til count carts) except res;
];
-1","sv string reverse first carts[;0 1];
/ 91,72