-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_20.rs
285 lines (266 loc) · 8.33 KB
/
day_20.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
277
278
279
280
281
282
283
284
285
//! https://adventofcode.com/2018/day/20
//! https://adventofcode.com/2018/day/20/input
use std::{
collections::{HashMap, HashSet},
fs::read_to_string,
time::Instant,
};
type Coord = (isize, isize);
#[derive(Clone, Debug)]
enum Match {
N,
E,
S,
W,
Many(Vec<Match>),
Or(Vec<Match>),
None,
}
impl Match {
fn parse(chars: Vec<char>) -> Self {
fn push(options: &mut Vec<Match>, option: &mut Vec<Match>) {
match option.len() {
0 => {
options.push(Match::None);
}
1 => {
options.push(option.pop().unwrap());
}
_ => {
options.push(Match::Many(option.clone()));
option.clear()
}
}
}
fn internal(chars: &Vec<char>, mut i: usize) -> (Match, usize) {
let mut options = Vec::new();
let mut option = Vec::new();
loop {
i = match chars[i] {
'|' => {
push(&mut options, &mut option);
i
}
'(' => {
let (group, i) = internal(chars, i + 1);
match group {
Match::Many(others) => option.extend(others),
_ => option.push(group),
}
i
}
')' | '$' => {
push(&mut options, &mut option);
break;
}
literal => {
option.push(Match::from(literal));
i
}
} + 1;
}
(
match options.len() {
0 => Match::None,
1 => options.pop().unwrap(),
_ => Match::Or(options),
},
i,
)
}
internal(&chars, 0).0
}
}
impl From<char> for Match {
fn from(char: char) -> Self {
match char {
'N' => Match::N,
'E' => Match::E,
'S' => Match::S,
'W' => Match::W,
_ => panic!("Invalid match: {}", char),
}
}
}
fn parse(input: &str) -> Match {
Match::parse(input.chars().skip(1).collect())
}
#[allow(unused)]
fn to_string(graph: &HashMap<Coord, HashSet<Coord>>) -> String {
let mut min_i = 0;
let mut max_i = 0;
let mut min_j = 0;
let mut max_j = 0;
for ((i, j), rooms) in graph.iter() {
min_i = min_i.min(*i);
max_i = max_i.max(*i);
min_j = min_j.min(*j);
max_j = max_j.max(*j);
for (i, j) in rooms {
min_i = min_i.min(*i);
max_i = max_i.max(*i);
min_j = min_j.min(*j);
max_j = max_j.max(*j);
}
}
let width = (max_j - min_j + 1) as usize;
let height = (max_i - min_i + 1) as usize;
let mut matrix = vec![vec!['.'; width * 2 + 1]; height * 2 + 1];
for i in 0..matrix.len() {
for j in 0..matrix[0].len() {
if i == matrix.len() - 1 || j == matrix[0].len() - 1 || i % 2 == 0 || j % 2 == 0 {
matrix[i][j] = '#';
}
}
}
for ((i, j), rooms) in graph.iter() {
let fi = (i - min_i) as usize * 2 + 1;
let fj = (j - min_j) as usize * 2 + 1;
for (i, j) in rooms {
let ti = (i - min_i) as usize * 2 + 1;
let tj = (j - min_j) as usize * 2 + 1;
matrix[(fi + ti) / 2][(fj + tj) / 2] = if fi == ti { '|' } else { '-' };
}
}
matrix[-min_i as usize * 2 + 1][-min_j as usize * 2 + 1] = 'X';
matrix
.iter()
.map(|row| row.iter().collect::<String>())
.collect::<Vec<String>>()
.join("\n")
}
fn explore(regex: &Match, coord: Coord) -> HashMap<Coord, HashSet<Coord>> {
fn mirror(graph: &mut HashMap<Coord, HashSet<Coord>>) {
for ((i, j), rooms) in graph.clone().iter() {
for (ri, rj) in rooms {
graph
.entry((*ri, *rj))
.or_insert_with(HashSet::new)
.insert((*i, *j));
}
}
}
fn internal(
regex: &Match,
coord @ (i, j): Coord,
graph: &mut HashMap<Coord, HashSet<Coord>>,
) -> HashSet<Coord> {
match regex {
Match::N => {
let next = (i - 1, j);
graph.entry(coord).or_insert_with(HashSet::new).insert(next);
HashSet::from([next])
}
Match::E => {
let next = (i, j + 1);
graph.entry(coord).or_insert_with(HashSet::new).insert(next);
HashSet::from([next])
}
Match::S => {
let next = (i + 1, j);
graph.entry(coord).or_insert_with(HashSet::new).insert(next);
HashSet::from([next])
}
Match::W => {
let next = (i, j - 1);
graph.entry(coord).or_insert_with(HashSet::new).insert(next);
HashSet::from([next])
}
Match::Many(many) => {
let mut next = HashSet::from([coord]);
for path in many {
next = internal(path, next.into_iter().next().unwrap(), graph);
}
next
}
Match::Or(options) => {
let mut next = HashSet::new();
for path in options {
next.extend(internal(path, coord, graph));
}
next
}
Match::None => HashSet::from([coord]),
}
}
let mut result = HashMap::new();
internal(regex, coord, &mut result);
mirror(&mut result);
result
}
pub mod part1 {
use std::collections::{HashMap, HashSet, VecDeque};
use super::{explore, parse, Coord};
fn bfs(graph: &HashMap<Coord, HashSet<Coord>>, start: Coord) -> usize {
let mut queue = VecDeque::from([start]);
let mut visited = HashSet::new();
let mut distance = 0;
while !queue.is_empty() {
let mut next = VecDeque::new();
for coord in queue {
if visited.contains(&coord) {
continue;
}
visited.insert(coord);
if let Some(rooms) = graph.get(&coord) {
next.extend(rooms);
}
}
queue = next;
distance += 1;
}
distance
}
pub fn solve(input: &str) -> usize {
let regex = parse(input);
let graph = explore(®ex, (0, 0));
bfs(&graph, (0, 0)) - 2
}
}
pub mod part2 {
use std::collections::{HashMap, HashSet, VecDeque};
use super::{explore, parse, Coord};
fn bfs(graph: &HashMap<Coord, HashSet<Coord>>, start: Coord) -> usize {
let mut at_least_1000 = 0;
let mut queue = VecDeque::from([start]);
let mut visited = HashSet::new();
let mut distance = 0;
while !queue.is_empty() {
let mut next = VecDeque::new();
for coord in queue {
if visited.contains(&coord) {
continue;
}
visited.insert(coord);
if distance >= 1000 {
at_least_1000 += 1;
}
if let Some(rooms) = graph.get(&coord) {
next.extend(rooms);
}
}
queue = next;
distance += 1;
}
at_least_1000
}
pub fn solve(input: &str) -> usize {
let regex = parse(input);
let graph = explore(®ex, (0, 0));
bfs(&graph, (0, 0))
}
}
pub fn main(test: bool) {
let test_input = "^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$".to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2018/day_20_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}