-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.rs
231 lines (206 loc) · 8.58 KB
/
day3.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
use aoc_runner_derive::aoc;
#[aoc(day3, part1)]
pub fn part1(input: &str) -> u32 {
// Sum of all numbers that are surrounded by symbols
let mut sum = 0;
// Iterate over the lines in the input
for (current_line_index, current_line) in input.lines().enumerate() {
let line_max_index = current_line.len().saturating_sub(1);
// Try getting the lines above and below the current line
let line_above = current_line_index
.checked_sub(1)
.and_then(|line_above_index| input.lines().nth(line_above_index));
let line_below = input.lines().nth(current_line_index.saturating_add(1));
// Iterate over the characters in the line and find the numbers
let mut num_start = None;
for (index, c) in current_line.char_indices() {
// Mark the start a new number and continue until its end or the end of the line is reached
let index_after_num = if c.is_ascii_digit() {
if num_start.is_none() {
num_start = Some(index);
}
if index == line_max_index {
index.saturating_add(1)
} else {
continue;
}
} else {
index
};
// The full number is found once a non-digit character is encountered after start of the number was marked
if let Some(number_index_start) = num_start {
// Determine the index of the character before the number (for diagonal checks)
let index_before_num = number_index_start.checked_sub(1);
// Check if the number should be added to the sum
#[allow(unused_parens)]
if (
// Character to the left of the number
index_before_num.is_some_and(|index_before_num| {
utils::is_special_symbol(current_line.chars().nth(index_before_num).unwrap())
})
// Character to the right of the number
|| current_line
.chars()
.nth(index_after_num).is_some_and(utils::is_special_symbol)
// Characters above the number
|| line_above.is_some_and(|line_above| {
line_above[index_before_num.unwrap_or(0)..index_after_num.saturating_add(1).min(line_max_index)].chars()
.any(utils::is_special_symbol)
})
// Characters below the number
|| line_below.is_some_and(|line_below| {
line_below[index_before_num.unwrap_or(0)..index_after_num.saturating_add(1).min(line_max_index)].chars()
.any(utils::is_special_symbol)
})
) {
// Add the number to the sum
sum += current_line[number_index_start..index_after_num]
.parse::<u32>()
.unwrap();
}
// Reset the start of the number
num_start = None;
}
}
}
sum
}
#[aoc(day3, part2)]
pub fn part2(input: &str) -> u32 {
// Sum of all products between two numbers that are connected by a gear ('*')
let mut sum = 0;
// Find all numbers adjacent to the gear
let mut adjacent_numbers = smallvec::SmallVec::<[u32; 2]>::new();
// Iterate over the lines in the input
for (current_line_index, current_line) in input.lines().enumerate() {
// Skip lines that do not contain any gears
if !current_line.contains(utils::is_gear) {
continue;
}
// Try getting the lines above and below the current line
let line_above = current_line_index
.checked_sub(1)
.and_then(|line_above_index| input.lines().nth(line_above_index));
let line_below = input.lines().nth(current_line_index.saturating_add(1));
// Iterate over the characters in the line and find the gears
for (index, c) in current_line.char_indices() {
// Skip non-gear characters
if !utils::is_gear(c) {
continue;
}
// Search for numbers to the left of the gear
if let Some(number) = current_line[..index]
.char_indices()
.rev()
.take_while(|(_, c)| c.is_ascii_digit())
.last()
.and_then(|(index, _)| {
current_line[index..]
.chars()
.take_while(char::is_ascii_digit)
.collect::<String>()
.parse::<u32>()
.ok()
})
{
adjacent_numbers.push(number);
}
// Search for numbers to the right of the gear
if let Ok(number) = current_line[(index + 1)..]
.chars()
.take_while(char::is_ascii_digit)
.collect::<String>()
.parse::<u32>()
{
adjacent_numbers.push(number);
}
// Search for numbers above the gear
if let Some(line_above) = line_above {
// Find the left-most digit above the gear
let numbers_start_index = line_above[..=index.saturating_sub(1)]
.char_indices()
.rev()
.take_while(|(_, c)| c.is_ascii_digit())
.last()
.map_or(index.saturating_sub(1), |(i, _)| i);
// Find the right-most digit above the gear
let numbers_end_index = line_above[index.saturating_add(1)..]
.char_indices()
.take_while(|(_, c)| c.is_ascii_digit())
.last()
.map_or(index.saturating_add(1), |(i, _)| {
index.saturating_add(2) + i
});
// Parse all numbers above the gear
line_above[numbers_start_index..numbers_end_index]
.split(|c: char| !c.is_ascii_digit())
.filter_map(|number| number.parse::<u32>().ok())
.for_each(|number| adjacent_numbers.push(number));
};
// Search for numbers below the gear
if let Some(line_below) = line_below {
// Find the left-most digit below the gear
let numbers_start_index = line_below[..=index.saturating_sub(1)]
.char_indices()
.rev()
.take_while(|(_, c)| c.is_ascii_digit())
.last()
.map_or(index.saturating_sub(1), |(i, _)| i);
// Find the right-most digit below the gear
let numbers_end_index = line_below[index.saturating_add(1)..]
.char_indices()
.take_while(|(_, c)| c.is_ascii_digit())
.last()
.map_or(index.saturating_add(1), |(i, _)| {
index.saturating_add(2) + i
});
// Parse all numbers below the gear
line_below[numbers_start_index..numbers_end_index]
.split(|c: char| !c.is_ascii_digit())
.filter_map(|number| number.parse::<u32>().ok())
.for_each(|number| adjacent_numbers.push(number));
}
// Add the product of the adjacent numbers to the sum if there are exactly two adjacent numbers
if adjacent_numbers.len() == 2 {
sum += adjacent_numbers[0] * adjacent_numbers[1];
}
adjacent_numbers.clear();
}
}
sum
}
mod utils {
/// Returns `true` if the character is a special symbol (excluding '.').
pub fn is_special_symbol(c: char) -> bool {
c != '.' && c.is_ascii_graphic()
}
/// Returns `true` if the character is a gear ('*').
pub fn is_gear(c: char) -> bool {
c == '*'
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 4361);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 467_835);
}
}