generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
217 lines (203 loc) · 5.98 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
use std::collections::{HashMap, HashSet};
advent_of_code::solution!(3);
enum Item {
Empty,
Symbol(char),
Number(u32),
}
impl Item {
fn from_char(c: char) -> Self {
if let Some(d) = c.to_digit(10) {
Item::Number(d)
} else if c == '.' {
Item::Empty
} else {
Item::Symbol(c)
}
}
fn is_symbol(&self) -> bool {
matches!(self, Item::Symbol(_))
}
fn is_star(&self) -> bool {
matches!(self, Item::Symbol('*'))
}
}
fn has_adjacent_symbol(row: usize, col: usize, graph: &Vec<Vec<Item>>) -> bool {
let rows = graph.len();
let cols = graph[0].len();
if col > 0 {
if graph[row][col - 1].is_symbol() {
return true;
};
if row > 0 && graph[row - 1][col - 1].is_symbol() {
return true;
}
if row < rows - 1 && graph[row + 1][col - 1].is_symbol() {
return true;
}
}
if col < cols - 1 {
if graph[row][col + 1].is_symbol() {
return true;
};
if row > 0 && graph[row - 1][col + 1].is_symbol() {
return true;
}
if row < rows - 1 && graph[row + 1][col + 1].is_symbol() {
return true;
}
}
if row > 0 && graph[row - 1][col].is_symbol() {
return true;
}
if row < rows - 1 && graph[row + 1][col].is_symbol() {
return true;
}
false
}
fn get_part_numbers(graph: &Vec<Vec<Item>>, row: usize) -> Vec<u32> {
let line = &graph[row];
let mut part_numbers = Vec::new();
let mut curr_num = 0;
let mut is_part_num = false;
for (col, item) in line.iter().enumerate() {
match item {
Item::Number(x) => {
curr_num = curr_num * 10 + x;
if !is_part_num {
is_part_num = has_adjacent_symbol(row, col, graph);
}
}
_ => {
if curr_num > 0 && is_part_num {
part_numbers.push(curr_num);
}
curr_num = 0;
is_part_num = false;
}
}
}
if curr_num > 0 && is_part_num {
part_numbers.push(curr_num);
}
part_numbers
}
pub fn part_one(input: &str) -> Option<u32> {
let graph: Vec<Vec<Item>> = input
.lines()
.map(|line| line.chars().map(Item::from_char).collect())
.collect();
let mut sum: u32 = 0;
for row in 0..graph.len() {
sum += get_part_numbers(&graph, row).iter().sum::<u32>();
}
Some(sum)
}
fn get_adjacent_stars(row: usize, col: usize, graph: &Vec<Vec<Item>>) -> HashSet<(usize, usize)> {
let mut adjacent_stars = HashSet::new();
let rows = graph.len();
let cols = graph[0].len();
if col > 0 {
if graph[row][col - 1].is_star() {
adjacent_stars.insert((row, col - 1));
};
if row > 0 && graph[row - 1][col - 1].is_star() {
adjacent_stars.insert((row - 1, col - 1));
}
if row < rows - 1 && graph[row + 1][col - 1].is_star() {
adjacent_stars.insert((row + 1, col - 1));
}
}
if col < cols - 1 {
if graph[row][col + 1].is_star() {
adjacent_stars.insert((row, col + 1));
};
if row > 0 && graph[row - 1][col + 1].is_star() {
adjacent_stars.insert((row - 1, col + 1));
}
if row < rows - 1 && graph[row + 1][col + 1].is_star() {
adjacent_stars.insert((row + 1, col + 1));
}
}
if row > 0 && graph[row - 1][col].is_star() {
adjacent_stars.insert((row - 1, col));
}
if row < rows - 1 && graph[row + 1][col].is_star() {
adjacent_stars.insert((row + 1, col));
}
adjacent_stars
}
fn add_numbers_to_gear_map(
graph: &Vec<Vec<Item>>,
gear_map: &mut HashMap<(usize, usize), Vec<u32>>,
row: usize,
) {
let line = &graph[row];
let mut curr_num = 0;
let mut adjacent_stars = HashSet::new();
for (col, item) in line.iter().enumerate() {
match item {
Item::Number(x) => {
curr_num = curr_num * 10 + x;
adjacent_stars.extend(get_adjacent_stars(row, col, graph));
}
_ => {
if curr_num != 0 {
adjacent_stars.iter().for_each(|point| {
gear_map
.get_mut(point)
.expect("Star points should exist in gear_map")
.push(curr_num);
});
}
curr_num = 0;
adjacent_stars = HashSet::new();
}
}
}
if curr_num != 0 {
adjacent_stars.iter().for_each(|point| {
gear_map
.get_mut(point)
.expect("Star points should exist in gear_map")
.push(curr_num);
});
}
}
pub fn part_two(input: &str) -> Option<u32> {
let graph: Vec<Vec<Item>> = input
.lines()
.map(|line| line.chars().map(Item::from_char).collect())
.collect();
let mut gear_map: HashMap<(usize, usize), Vec<u32>> = HashMap::new();
graph.iter().enumerate().for_each(|(row, line)| {
line.iter().enumerate().for_each(|(col, item)| {
if item.is_star() {
gear_map.insert((row, col), Vec::new());
}
})
});
for row in 0..graph.len() {
add_numbers_to_gear_map(&graph, &mut gear_map, row);
}
let sum = gear_map
.values()
.filter(|nums| nums.len() == 2)
.map(|nums| nums[0] * nums[1])
.sum();
Some(sum)
}
#[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));
}
}