-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.rs
278 lines (237 loc) · 7.33 KB
/
day16.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
268
269
270
271
272
273
274
275
276
use std::{collections::HashSet};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Direction {
North,
Eastt,
South,
Westt,
}
use Direction::*;
impl Direction {
fn step(&self, other: (i32, i32)) -> Option<(i32, i32)> {
return match self {
North => Some((other.0.checked_sub(1)?, other.1)),
Eastt => Some((other.0, other.1.checked_add(1)?)),
South => Some((other.0.checked_add(1)?, other.1)),
Westt => Some((other.0, other.1.checked_sub(1)?)),
};
}
}
#[derive(Debug, Clone)]
enum TileType {
Empty,
Mirror1,
Mirror2,
SplitterH,
SplitterV,
}
impl From<char> for TileType {
fn from(value: char) -> Self {
match value {
'.' => Self::Empty,
'/' => Self::Mirror1,
'\\' => Self::Mirror2,
'-' => Self::SplitterH,
'|' => Self::SplitterV,
_ => panic!("In the disco!"),
}
}
}
#[derive(Debug, Clone)]
struct Tile {
t: TileType,
visited: HashSet<Direction>,
}
impl std::fmt::Display for Tile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.t {
TileType::Mirror1 => f.write_str("/"),
TileType::Mirror2 => f.write_str("\\"),
TileType::SplitterH => f.write_str("–"),
TileType::SplitterV => f.write_str("|"),
TileType::Empty => match self.visited.len() {
0 => f.write_str("·"),
1 => match self.visited.iter().next().unwrap() {
North => f.write_str("↑"),
Eastt => f.write_str("→"),
South => f.write_str("↓"),
Westt => f.write_str("←"),
},
x => f.write_str(&x.to_string())
},
}
}
}
impl From<char> for Tile {
fn from(value: char) -> Self {
Self {
t: TileType::from(value),
visited: HashSet::new(),
}
}
}
#[derive(Clone)]
struct Grid(Vec<Vec<Tile>>);
impl std::fmt::Display for Grid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for row in &self.0 {
for tile in row {
tile.fmt(f)?;
}
writeln!(f,"")?;
}
writeln!(f, "")
}
}
#[aoc_generator(day16)]
fn parser(input: &str) -> Grid {
Grid(input
.lines()
.map(|s| s.chars().map(Tile::from).collect())
.collect())
}
fn solver(mut grid: Grid, start: ((i32, i32), Direction)) -> usize {
let mut laszers: Vec<((i32, i32), Direction)> = vec![start];
while let Some(laser) = laszers.pop() {
let mut dir = laser.1;
let pos: Option<(i32, i32)> = dir.step(laser.0);
if pos.is_none() {
continue;
}
let pos = pos.unwrap();
let row: Option<&mut Vec<Tile>> = grid.0.get_mut(pos.0 as usize);
if row.is_none() {
continue;
}
let tile: Option<&mut Tile> = row.unwrap().get_mut(pos.1 as usize);
if tile.is_none() {
continue;
}
let tile = tile.unwrap();
if tile.visited.contains(&dir) {
continue;
}
tile.visited.insert(dir);
match tile.t {
TileType::Empty => laszers.push((pos, dir)),
TileType::Mirror1 => {
dir = match dir {
North => Eastt,
Eastt => North,
Westt => South,
South => Westt,
};
laszers.push((pos, dir))
}
TileType::Mirror2 => {
dir = match dir {
North => Westt,
Westt => North,
Eastt => South,
South => Eastt,
};
laszers.push((pos, dir))
}
TileType::SplitterH => match dir {
Eastt | Westt => laszers.push((pos, dir)),
North | South => {
laszers.push((pos, Eastt));
laszers.push((pos, Westt));
}
},
TileType::SplitterV => match dir {
North | South => laszers.push((pos, dir)),
Eastt | Westt => {
laszers.push((pos, North));
laszers.push((pos, South));
}
},
}
}
return grid.0.iter().map(|row| row.iter().filter(|x| !x.visited.is_empty()).count()).sum();
}
#[aoc(day16, part1)]
fn solver_part1(grid: &Grid) -> usize {
return solver(grid.clone(), ((0, -1), Eastt))
}
#[aoc(day16, part2, single)]
fn solver_part2_single(grid: &Grid) -> usize {
let w: i32 = grid.0.first().unwrap().len() as i32;
let h: i32 = grid.0.len() as i32;
let mut starts: Vec<((i32, i32), Direction)> = Vec::new();
for i in 0..h {
starts.push(((i as i32, -1), Eastt));
starts.push(((i as i32, w), Westt));
}
for i in 0..w {
starts.push(((-1, i), South));
starts.push(((h, i), North));
}
return starts.iter().map(|s| solver(grid.clone(), *s)).max().unwrap();
}
#[aoc(day16, part2, stupid)]
fn solver_part2_stupid_multi(grid: &Grid) -> usize {
let w: i32 = grid.0.first().unwrap().len() as i32;
let h: i32 = grid.0.len() as i32;
let mut starts: Vec<((i32, i32), Direction)> = Vec::new();
for i in 0..h {
starts.push(((i as i32, -1), Eastt));
starts.push(((i as i32, w), Westt));
}
for i in 0..w {
starts.push(((-1, i), South));
starts.push(((h, i), North));
}
let mut handles = vec![];
for start in starts {
let l_grid = grid.clone();
let handle = std::thread::spawn(move || solver(l_grid, start));
handles.push(handle);
}
let mut results = vec![];
for handle in handles {
results.push(handle.join().unwrap());
}
return *results.iter().max().unwrap();
}
#[aoc(day16, part2, multi)]
fn solver_part2_multi(grid: &Grid) -> usize {
let grid = grid.clone();
let w: i32 = grid.0.first().unwrap().len() as i32;
let h: i32 = grid.0.len() as i32;
let mut handles = vec![];
let g = grid.clone();
let handle = std::thread::spawn(move || {
(0..h).map(|i| solver(g.clone(), ((i, -1), Eastt))).max().unwrap()
});
handles.push(handle);
let g = grid.clone();
let handle = std::thread::spawn(move || {
(0..h).map(|i| solver(g.clone(), ((i, w), Westt))).max().unwrap()
});
handles.push(handle);
let g = grid.clone();
let handle = std::thread::spawn(move || {
(0..w).map(|i| solver(g.clone(), ((-1, i), South))).max().unwrap()
});
handles.push(handle);
let g = grid.clone();
let handle = std::thread::spawn(move || {
(0..w).map(|i| solver(g.clone(), ((h, i), North))).max().unwrap()
});
handles.push(handle);
let mut results = vec![];
for handle in handles {
results.push(handle.join().unwrap());
}
return *results.iter().max().unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_1: &str = ".|...\\....\n|.-.\\.....\n.....|-...\n........|.\n..........\n.........\\\n..../.\\\\..\n.-.-/..|..\n.|....-|.\\\n..//.|....";
#[test]
fn test_solver_part1() {
assert_eq!(5, solver_part1(&parser(EXAMPLE_1)));
}
}