-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_08.rs
267 lines (253 loc) · 6.1 KB
/
day_08.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::prelude::*;
use std::collections::{HashMap, HashSet};
pub struct Day08;
impl Puzzle for Day08 {
fn new(_ops: &super::RootOpt) -> Box<dyn Puzzle> {
Box::new(Self)
}
fn part_one(&self, _input: &str) -> super::PuzzleResult {
let radios = RadioMap::new(_input, PuzzlePart::P1);
println!("{}", radios.to_string());
return Ok(radios.antinode_locations.len().to_string());
}
fn part_two(&self, _input: &str) -> super::PuzzleResult {
let radios = RadioMap::new(_input, PuzzlePart::P2);
println!("{}", radios.to_string());
return Ok(radios.antinode_locations.len().to_string());
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum CellConfigValue {
#[default]
Unknown,
Antenna(char),
Obstacle,
}
impl std::str::FromStr for CellConfigValue {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"" => Ok(CellConfigValue::Unknown),
"." => Ok(CellConfigValue::Unknown),
_ => Ok(CellConfigValue::Antenna(s.chars().nth(0).unwrap())),
}
}
}
impl CellConfigValue {
fn to_string(&self) -> String {
match self {
CellConfigValue::Unknown => String::from("."),
CellConfigValue::Antenna(c) => c.to_string(),
CellConfigValue::Obstacle => String::from("#"),
}
}
}
fn parse_map(input: &str) -> FBGrid<CellConfigValue> {
let parsed = input
.trim()
.split("\n")
.map(|line| {
line.chars()
.filter_map(|c| CellConfigValue::from_str(&c.to_string()).ok())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
return FBGrid {
grid: grid_from_vec_vec(parsed),
};
}
impl FBGrid<CellConfigValue> {
fn to_string(&self) -> String {
// let gs = self.grid.to_string()
let mut result = String::new();
for row in self.grid.iter_rows() {
for cell in row {
result.push_str(&cell.to_string());
}
result.push('\n');
}
return result;
}
fn make_antinode_left(
&self,
a: &GridCoord2,
_b: &GridCoord2,
dist: GridDistance2<i32>,
) -> Option<GridCoord2> {
let r0 = (a.0 as i32).checked_sub(dist.0)?;
let r1 = (a.1 as i32).checked_sub(dist.1)?;
if r0 < 0 || r1 < 0 {
return None;
}
if r0 as usize > self.grid.rows() || r1 as usize > self.grid.cols() {
return None;
}
return Some((r0 as usize, r1 as usize));
}
fn make_antinode_right(
&self,
_a: &GridCoord2,
b: &GridCoord2,
dist: GridDistance2<i32>,
) -> Option<GridCoord2> {
let r0 = (b.0 as i32) + (dist.0);
let r1 = (b.1 as i32) + (dist.1);
if r0 < 0 || r1 < 0 {
return None;
}
if r0 as usize > self.grid.rows() || r1 as usize > self.grid.cols() {
return None;
}
return Some((r0 as usize, r1 as usize));
}
}
struct RadioMap {
grid: FBGrid<CellConfigValue>,
// antenna_locations: HashMap<char, Vec<GridCoord2>>,
antinode_locations: Vec<GridCoord2>,
}
enum PuzzlePart {
P1,
P2,
}
impl RadioMap {
fn new(input: &str, part: PuzzlePart) -> Self {
let grid = parse_map(input);
let mut antenna_locations: HashMap<char, Vec<GridCoord2>> = HashMap::new();
let mut antinode_locations: HashSet<GridCoord2> = HashSet::new();
grid.grid.indexed_iter().for_each(|(loc, val)| match val {
CellConfigValue::Antenna(c) => {
if !antenna_locations.contains_key(c) {
antenna_locations.insert(*c, Vec::new());
}
antenna_locations.get_mut(c).unwrap().push(loc);
}
_ => {}
});
match part {
PuzzlePart::P1 => {
antenna_locations
.values()
.map(|locations| {
return locations
.iter()
.combinations(2)
.map(|combo| {
let dist = grid.distance(combo[0], combo[1]);
vec![
grid.make_antinode_left(combo[0], combo[1], dist),
grid.make_antinode_right(combo[0], combo[1], dist),
]
})
.flatten()
.filter_map(|n| {
if let Some(coord) = n {
if let Some(_) = grid.grid.get(coord.0, coord.1) {
return n;
}
}
return None;
})
.collect::<Vec<GridCoord2>>();
})
.flatten()
.for_each(|loc| {
antinode_locations.insert(loc);
});
}
PuzzlePart::P2 => {
antenna_locations
.values()
.map(|locations| {
return locations
.iter()
.combinations(2)
.map(|combo| {
let dist = grid.distance(combo[0], combo[1]).normalize();
let mut curr = *combo[0];
let mut batch: Vec<GridCoord2> = Vec::new();
// Include the starting node in the batch as make_antinode_left and make_antinode_right would skip it
batch.push(curr);
while let Some(next) =
grid.make_antinode_left(&curr, combo[1], dist)
{
batch.push(next);
curr = next;
}
// Go to the right from the left starting point to catch points between the antennas
curr = *combo[0];
while let Some(next) =
grid.make_antinode_right(combo[0], &curr, dist)
{
batch.push(next);
curr = next;
}
return batch;
})
.flatten()
.filter_map(|n| {
if let Some(_) = grid.grid.get(n.0, n.1) {
return Some(n);
}
return None;
})
.collect::<Vec<GridCoord2>>();
})
.flatten()
.for_each(|loc| {
antinode_locations.insert(loc);
});
}
}
return Self {
grid,
// antenna_locations,
antinode_locations: antinode_locations.iter().map(|n| *n).collect::<Vec<_>>(),
};
}
fn to_string(&self) -> String {
let mut visual = self.grid.clone();
self.antinode_locations.iter().for_each(|(r, c)| {
if let Some(cell) = visual.grid.get_mut(*r, *c) {
*cell = CellConfigValue::Obstacle;
}
});
return format!("{}\n--\n{}", self.grid.to_string(), visual.to_string());
}
}
#[test]
fn sample_day08_1() {
let input = r#"
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"#;
println!("{:?}", Day08.part_one(input).unwrap());
}
#[test]
fn sample_day08_2() {
let input = r#"
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"#;
println!("{:?}", Day08.part_two(input).unwrap());
}