-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.rs
37 lines (32 loc) · 850 Bytes
/
main.rs
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
#![feature(array_chunks)]
mod bits;
use bits::Bits;
pub fn main() {
let bytes = include_str!("../input.txt")
.trim()
.as_bytes()
.array_chunks()
.map(|&[a, b]| {
(((a as char).to_digit(16).unwrap() as u8) << 4)
| (b as char).to_digit(16).unwrap() as u8
})
.collect::<Vec<_>>();
println!("{}", packet(&mut Bits::new(&bytes)));
}
fn packet(bits: &mut Bits) -> usize {
let mut ver = bits.take(3);
if bits.take(3) == 4 {
bits.take_literal();
return ver;
}
if bits.take(1) == 0 {
let len = bits.take(15);
let mut payload = bits.split(len);
while !payload.is_empty() {
ver += packet(&mut payload);
}
} else {
(0..bits.take(11)).for_each(|_| ver += packet(bits));
}
ver
}