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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
use advent_lib::prelude::*;
#[derive(Debug)]
struct Packet {
version: u8,
type_id: u8,
content: Vec<u8>,
subpackets: Vec<Packet>,
}
fn main() -> Result<()> {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: advent input");
}
let _ = args.next();
let filename = args.next().unwrap();
let input = advent_lib::read_lines_file(&filename)?;
let mut bit_string: Vec<u8> = Vec::new();
for c in input[0].chars() {
let value = c.to_digit(16).unwrap() as u8;
for i in 0 .. 4 {
let bit = (value >> (3 - i)) & 1;
bit_string.push(bit);
}
}
let mut iter = bit_string.iter().peekable();
let (version_total, _top_packet, result) = read_packet(&mut iter).unwrap();
println!("{}", version_total);
println!("{}", result);
Ok(())
}
fn read_packet<'a>(iter: &mut impl Iterator<Item=&'a u8>) -> Result<(u64, Packet, u128)>
{
let mut version_total: u64 = 0;
let version = bits_to_u8(take_bits(iter, 3)?);
let type_id = bits_to_u8(take_bits(iter, 3)?);
version_total += version as u64;
if type_id == 4 {
let mut result = 0;
loop {
let bits = take_bits(iter, 5)?;
let bits_value = bits_to_u128(bits[1..].to_vec());
result = result << 4;
result += bits_value;
if bits[0] == 0 {
break;
}
}
Ok((version_total, Packet {
version,
type_id,
content: Vec::new(),
subpackets: Vec::new(),
}, result))
} else {
let mut subpackets = Vec::new();
let mut parameters = Vec::new();
let length_type_id = bits_to_u8(take_bits(iter, 1)?);
if length_type_id == 0 {
let length_in_bits = bits_to_u64(take_bits(iter, 15)?);
let mut content_bits: Vec<u8> = Vec::new();
for bit in iter.take(length_in_bits as usize) {
content_bits.push(*bit);
}
let mut content_bits = content_bits.iter().peekable();
loop {
match read_packet(&mut content_bits) {
Err(_) => {
break;
}
Ok((sub_version_total, subpacket, value)) => {
version_total += sub_version_total;
subpackets.push(subpacket);
parameters.push(value);
}
}
}
} else {
let n_subpackets = bits_to_u64(take_bits(iter, 11)?);
for _ in 0 .. n_subpackets {
match read_packet(iter) {
Err(_) => {
return Err(advent_lib::error::Error::Unspecified);
}
Ok((sub_version_total, subpacket, value)) => {
version_total += sub_version_total;
subpackets.push(subpacket);
parameters.push(value);
}
}
}
}
let mut result = 0;
match type_id {
0 => {
result = 0;
for parameter in ¶meters {
result += parameter;
}
}
1 => {
result = 1;
for parameter in ¶meters {
result *= parameter;
}
}
2 => {
result = parameters[0];
for parameter in ¶meters {
if *parameter < result {
result = *parameter;
}
}
}
3 => {
result = parameters[0];
for parameter in ¶meters {
if *parameter > result {
result = *parameter;
}
}
}
5 => {
result = if parameters[0] > parameters[1] { 1 } else { 0 };
}
6 => {
result = if parameters[0] < parameters[1] { 1 } else { 0 };
}
7 => {
result = if parameters[0] == parameters[1] { 1 } else { 0 };
}
_ => { }
}
Ok((version_total, Packet {
version,
type_id,
content: Vec::new(),
subpackets: subpackets,
}, result))
}
}
fn take_bits<'a>(iter: &mut impl Iterator<Item=&'a u8>, count: usize)
-> Result<Vec<u8>>
{
let mut result: Vec<u8> = Vec::new();
for _ in 0 .. count {
match iter.next() {
None => {
return Err(advent_lib::error::Error::Unspecified);
}
Some(bit) => {
result.push(*bit);
}
}
}
Ok(result)
}
fn bits_to_u8(bits: Vec<u8>) -> u8 {
let mut result = 0;
for bit in &bits {
result *= 2;
result += bit;
}
result
}
fn bits_to_u64(bits: Vec<u8>) -> u64 {
let mut result = 0;
for bit in &bits {
result *= 2;
result += *bit as u64;
}
result
}
fn bits_to_u128(bits: Vec<u8>) -> u128 {
let mut result = 0;
for bit in &bits {
result *= 2;
result += *bit as u128;
}
result
}
|