generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rs
241 lines (195 loc) · 6.53 KB
/
03.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
advent_of_code::solution!(3);
pub fn part_one(input: &str) -> Option<u32> {
let mut sum = 0;
let mut window = EngineSchematicWindow::new();
for line in input.lines().chain("\n\n".lines()) {
window.add_row_from_line(line);
window.mark_part_numbers();
sum += window.sum_row_bottom_part_numbers();
}
Some(sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut sum = 0;
let mut window = EngineSchematicWindow::new();
for line in input.lines().chain("\n\n".lines()) {
window.add_row_from_line(line);
sum += window.sum_gear_ratios();
}
Some(sum)
}
struct EngineSchematicWindow {
overflow_index: usize,
rows: [EngineSchematicRow; 3],
}
impl EngineSchematicWindow {
fn new() -> EngineSchematicWindow {
EngineSchematicWindow {
overflow_index: 0,
rows: [
EngineSchematicRow::new(),
EngineSchematicRow::new(),
EngineSchematicRow::new(),
],
}
}
fn add_row_from_line(&mut self, line: &str) {
self.add_row(EngineSchematicRow::from_line(line));
}
fn add_row(&mut self, row: EngineSchematicRow) {
self.rows[self.overflow_index] = row;
let next_overflow_index = (self.overflow_index + 1) % self.rows.len();
self.overflow_index = next_overflow_index;
}
fn mark_part_numbers(&mut self) {
let symbol_ranges = self.get_symbol_ranges_from_middle_row();
if symbol_ranges.is_empty() {
return;
}
for row in self.rows.iter_mut() {
let mut symbol_iterator = symbol_ranges.iter();
let mut number_iterator = row.numbers.iter_mut();
let mut current_symbol = symbol_iterator.next();
let mut current_number = number_iterator.next();
while let (Some(symbol), Some(number)) = (¤t_symbol, &mut current_number) {
if number.range.start() > symbol.end() {
current_symbol = symbol_iterator.next();
continue;
}
if symbol.start() > number.range.end() {
current_number = number_iterator.next();
continue;
}
number.mark_as_part_number();
current_number = number_iterator.next();
}
}
}
fn get_symbol_ranges_from_middle_row(&self) -> Vec<std::ops::RangeInclusive<usize>> {
self.get_row_middle()
.symbol_indices
.iter()
.map(|index| index - 1..=index + 1)
.collect()
}
fn get_row_middle(&self) -> &EngineSchematicRow {
let index = (self.overflow_index + self.rows.len() - 2) % self.rows.len();
&self.rows[index]
}
fn sum_row_bottom_part_numbers(&self) -> u32 {
self.get_row_bottom()
.numbers
.iter()
.filter(|number| number.is_part_number)
.map(|number| number.value)
.sum()
}
fn get_row_bottom(&self) -> &EngineSchematicRow {
&self.rows[self.overflow_index]
}
fn sum_gear_ratios(&self) -> u32 {
let mut sum = 0;
for gear_range in self.get_gear_ranges_from_middle_row() {
let mut gear_numbers = Vec::new();
for row in self.rows.iter() {
for number in row.numbers.iter() {
if number.overlaps(&gear_range) {
gear_numbers.push(number.value);
}
}
}
if gear_numbers.len() == 2 {
sum += gear_numbers.iter().product::<u32>();
}
}
sum
}
fn get_gear_ranges_from_middle_row(&self) -> Vec<std::ops::RangeInclusive<usize>> {
self.get_row_middle()
.gear_symbol_indices
.iter()
.map(|index| index - 1..=index + 1)
.collect()
}
}
struct EngineSchematicRow {
numbers: Vec<EngineSchematicNumber>,
symbol_indices: Vec<usize>,
gear_symbol_indices: Vec<usize>,
}
impl EngineSchematicRow {
fn from_line(line: &str) -> EngineSchematicRow {
let mut row = EngineSchematicRow::new();
for (index, character) in line.chars().enumerate() {
if character.is_ascii_digit() {
let value = character.to_digit(10).unwrap();
let last_number_option = row.numbers.last_mut();
if let Some(last_number) = last_number_option {
if last_number.should_append(index) {
last_number.append(value);
} else {
row.numbers.push(EngineSchematicNumber::new(value, index));
}
} else {
row.numbers.push(EngineSchematicNumber::new(value, index));
}
} else if character != '.' {
row.symbol_indices.push(index);
if character == '*' {
row.gear_symbol_indices.push(index);
}
}
}
row
}
fn new() -> EngineSchematicRow {
EngineSchematicRow {
numbers: Vec::new(),
symbol_indices: Vec::new(),
gear_symbol_indices: Vec::new(),
}
}
}
struct EngineSchematicNumber {
value: u32,
range: std::ops::RangeInclusive<usize>,
is_part_number: bool,
}
impl EngineSchematicNumber {
fn new(value: u32, index: usize) -> EngineSchematicNumber {
EngineSchematicNumber {
value,
range: index..=index,
is_part_number: false,
}
}
fn append(&mut self, value: u32) {
self.value = self.value * 10 + value;
self.range = *self.range.start()..=self.range.end() + 1;
}
fn should_append(&self, index: usize) -> bool {
index == self.range.end() + 1
}
fn mark_as_part_number(&mut self) {
if !self.is_part_number {
self.is_part_number = true;
}
}
fn overlaps(&self, range: &std::ops::RangeInclusive<usize>) -> bool {
self.range.start() <= range.end() && self.range.end() >= range.start()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(4361));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(467835));
}
}