6 { ['0'] = "0000", ['1'] = "0001", ['2'] = "0010", ['3'] = "0011"
7 , ['4'] = "0100", ['5'] = "0101", ['6'] = "0110", ['7'] = "0111"
8 , ['8'] = "1000", ['9'] = "1001", ['A'] = "1010", ['B'] = "1011"
9 , ['C'] = "1100", ['D'] = "1101", ['E'] = "1110", ['F'] = "1111"
12 struct stream
{ int pos
; char *buf
; };
14 int next(struct stream
*f
)
17 f
->buf
= hex2bin
[getchar()];
18 int r
= *(f
->buf
++) == '1' ? 1 : 0;
23 unsigned long bin2int(struct stream
*f
, int n
)
26 for (int i
= 0; i
<n
; i
++)
31 unsigned long parse_packet(struct stream
*f
)
33 int packetversion
__attribute__((unused
)) = bin2int(f
, 3);
34 int packettype
= bin2int(f
, 3);
38 if (packettype
== 4) {
40 r
= r
*16+bin2int(f
, 4);
41 r
= r
*16+bin2int(f
, 4);
44 int lengthtypeid
= next(f
);
45 unsigned long packets
[100] = {0};
49 if (lengthtypeid
== 0) {
50 int lengthsubpackets
= bin2int(f
, 15);
52 while (f
->pos
- oldpos
< lengthsubpackets
)
53 packets
[npackets
++] = parse_packet(f
);
56 npackets
= bin2int(f
, 11);
57 for (int i
= 0; i
<npackets
; i
++)
58 packets
[i
] = parse_packet(f
);
61 if (packettype
== 0) { //sum
62 for (int i
= 0; i
<npackets
; i
++)
64 } else if (packettype
== 1) { //product
66 for (int i
= 0; i
<npackets
; i
++)
68 } else if (packettype
== 2) { //minimum
70 for (int i
= 0; i
<npackets
; i
++)
71 r
= packets
[i
] < r
? packets
[i
] : r
;
72 } else if (packettype
== 3) { //maximum
73 for (int i
= 0; i
<npackets
; i
++)
74 r
= packets
[i
] > r
? packets
[i
] : r
;
75 } else if (packettype
== 5) { //greater than
76 r
= packets
[0] > packets
[1];
77 } else if (packettype
== 6) { //less than
78 r
= packets
[0] < packets
[1];
79 } else if (packettype
== 7) { //equal to
80 r
= packets
[0] == packets
[1];
88 struct stream f
= {.pos
=0, .buf
=""};
89 printf("%lu\n", parse_packet(&f
));