-
Notifications
You must be signed in to change notification settings - Fork 1
/
12.q
46 lines (41 loc) · 839 Bytes
/
12.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
//--- Day 12: Rain Risk ---
f:{[pd;a]
p:first pd;
d:last pd;
if[a[0] in `N`E`S`W;
:(p+a[1]*(`N`S`E`W!(0 1;0 -1;1 0;-1 0)) a[0];d)
];
if[a[0]=`F;
:(p+a[1]*(`N`S`E`W!(0 1;0 -1;1 0;-1 0)) d;d)
];
if[a[0] in `L`R;
r:"j"$a[1] % 90;
:$[a[0]=`L;
(p;{(`N`E`S`W!`W`N`E`S) x}/[r;d]);
(p;{(`N`E`S`W!`E`S`W`N) x}/[r;d])
]
];
};
i:{(`$first x;"J"$1_x)} each read0 `:input/12.txt
sum abs first f/[(0 0;`E);i]
/1148
f:{[wp;a]
w:first wp;
p:last wp;
if[a[0] in `N`E`S`W;
:(w+a[1]*(`N`S`E`W!(0 1;0 -1;1 0;-1 0)) a[0];p)
];
if[a[0] = `F;
:(p+d;p:p+a[1]*d:w - p)
];
if[a[0] in `L`R;
r:"j"$a[1] % 90;
d:w - p;
:$[a[0]=`L;
(p+{[x] (-1*x[1];x[0]) }/[r;d];p);
(p+{[x] (x[1];-1*x[0]) }/[r;d];p)
]
];
};
sum abs last f/[(10 1;0 0);i]
/52203