-
Notifications
You must be signed in to change notification settings - Fork 1
/
16.q
55 lines (46 loc) · 1.12 KB
/
16.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
// --- Day 16: Packet Decoder ---
i:raze -4#'0b vs'"0123456789ABCDEF"?first read0`:input/16.txt
pt:(sum/;prd/;min/;max/;::;>/;</;~/)
V:0;
f:{
tvp:0 3 6 _ x; // type, version, packet
x:6 _ x; // drop header
V+:2 sv tvp 0; // FIXME
if[4=t:2 sv tvp 1;
p:count raze r:{ (1+sum mins first each x)#x:x where 5=count each x } 5 cut x;
:(p _ x;2 sv raze 1_'r)
];
r:();
$[first x;
[
// 11 bits - number of packets
np:2 sv 11#1 _ x;
// drop 11 + 1 bits
x:11 _ 1 _ x;
while[0 < np;
xy:.z.s x;
r,:last xy;
x:first xy;
np-:1
]
];
[
// 15 bits - total length
tl:2 sv 15#1 _ x;
// drop 15 + 1 bits
x:15 _ 1 _ x;
while[0 < tl;
cx:count x;
xy:.z.s x;
r,:last xy;
x:first xy;
tl:tl - cx - count x
]
]];
:(x;"j"$pt[t] r)
}
res:f[i]
V
/1014
last res
/1922490999789