-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.rs
434 lines (394 loc) · 12.9 KB
/
day10.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use crate::data::load;
use petgraph::algo::{self, DfsSpace};
use petgraph::{graph::NodeIndex, graph::UnGraph};
use std::collections::{HashMap, HashSet};
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum PuzzleErr {
#[error("Unknown pipe character: {}.", .0)]
UnknownPipeChar(String),
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
enum Pipe {
V,
H,
NE,
NW,
SW,
SE,
G,
S,
}
impl TryFrom<&char> for Pipe {
type Error = PuzzleErr;
fn try_from(value: &char) -> Result<Self, Self::Error> {
match value {
'|' => Ok(Pipe::V),
'-' => Ok(Pipe::H),
'L' => Ok(Pipe::NE),
'J' => Ok(Pipe::NW),
'7' => Ok(Pipe::SW),
'F' => Ok(Pipe::SE),
'.' => Ok(Pipe::G),
'S' => Ok(Pipe::S),
c => Err(PuzzleErr::UnknownPipeChar(c.to_string())),
}
}
}
impl Pipe {
fn is_turn(&self) -> bool {
matches!(self, Pipe::NE | Pipe::NW | Pipe::SW | Pipe::SE)
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct Coord {
r: i32,
c: i32,
}
#[derive(Debug, Clone)]
struct Maze {
map: HashMap<Coord, Pipe>,
graph: UnGraph<Coord, ()>,
start_i: NodeIndex,
node_indices: HashMap<Coord, NodeIndex>,
}
fn _build_map(input: &str) -> Result<HashMap<Coord, Pipe>, PuzzleErr> {
let mut map = HashMap::new();
for (r, line) in input.trim().lines().enumerate() {
for (c, x) in line.trim().chars().enumerate() {
map.insert(
Coord {
r: r as i32,
c: c as i32,
},
Pipe::try_from(&x)?,
);
}
}
Ok(map)
}
fn _add_neighbors(
graph: &mut UnGraph<Coord, ()>,
nodes: &HashMap<Coord, NodeIndex>,
node_idx: &NodeIndex,
coord: &Coord,
directions: &Vec<(i32, i32)>,
) {
for (dr, dc) in directions {
let neighbor = Coord {
r: coord.r + dr,
c: coord.c + dc,
};
if let Some(neighbor_i) = nodes.get(&neighbor) {
graph.add_edge(*node_idx, *neighbor_i, ());
}
}
}
fn _map_to_graph(
map: &HashMap<Coord, Pipe>,
) -> (UnGraph<Coord, ()>, HashMap<Coord, NodeIndex>, NodeIndex) {
let mut nodes = HashMap::new();
let mut graph = UnGraph::new_undirected();
let mut start_i: Option<NodeIndex> = None;
for (c, p) in map.iter() {
match p {
Pipe::G => (),
Pipe::S => {
start_i = Some(graph.add_node(*c));
nodes.insert(*c, start_i.unwrap());
}
_ => {
nodes.insert(*c, graph.add_node(*c));
}
};
}
for (coord, p) in map.iter() {
if let Some(node_idx) = nodes.get(coord) {
let directions = match p {
Pipe::V => Vec::from_iter([(-1, 0), (1, 0)]),
Pipe::H => Vec::from_iter([(0, -1), (0, 1)]),
Pipe::NE => Vec::from_iter([(-1, 0), (0, 1)]),
Pipe::NW => Vec::from_iter([(-1, 0), (0, -1)]),
Pipe::SW => Vec::from_iter([(1, 0), (0, -1)]),
Pipe::SE => Vec::from_iter([(1, 0), (0, 1)]),
Pipe::S => Vec::from_iter([(-1, 0), (1, 0), (0, -1), (0, 1)]),
Pipe::G => Vec::new(),
};
_add_neighbors(&mut graph, &nodes, node_idx, coord, &directions);
}
}
// Only keep edges between nodes with two edges at this point.
let node_indices = graph.node_indices().collect::<Vec<_>>();
let mut keep_edges = HashSet::new();
for (i, n1) in node_indices.iter().enumerate() {
for n2 in node_indices[(i + 1)..].iter() {
let edges = graph.edges_connecting(*n1, *n2).collect::<Vec<_>>();
if edges.len() == 2 {
keep_edges.insert(graph.find_edge(*n1, *n2).unwrap());
}
}
}
graph.retain_edges(|_, e| keep_edges.contains(&e));
// Only retain nodes connected to S.
let mut dfs_space = DfsSpace::default();
let keep_nodes = graph
.node_indices()
.filter(|n| algo::has_path_connecting(&graph, start_i.unwrap(), *n, Some(&mut dfs_space)))
.collect::<HashSet<_>>();
graph.retain_nodes(|_, n| keep_nodes.contains(&n));
(graph, nodes, start_i.unwrap())
}
fn parse_input(data: &str) -> Result<Maze, PuzzleErr> {
let map = _build_map(data)?;
let (graph, node_indices, start_i) = _map_to_graph(&map);
Ok(Maze {
map,
graph,
start_i,
node_indices,
})
}
pub fn puzzle_1(input: &str) -> Result<i32, PuzzleErr> {
let maze = parse_input(input)?;
Ok(*algo::dijkstra(&maze.graph, maze.start_i, None, |_| 1)
.values()
.max()
.unwrap())
}
fn _counter_turning_pipes(a: &Pipe, b: &Pipe) -> bool {
matches!(
(a, b),
(Pipe::NW, Pipe::SE) | (Pipe::SE, Pipe::NW) | (Pipe::SW, Pipe::NE) | (Pipe::NE, Pipe::SW)
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Decision {
Break,
Continue,
Insert,
UpdatePrevTurn,
}
fn collection_decision(
coord: &Coord,
pipe: &Pipe,
graph_nodes: &HashMap<Coord, NodeIndex>,
prev_turn: &Option<Pipe>,
straight_breaking_pipe: &Pipe,
) -> Decision {
if !graph_nodes.contains_key(coord) {
log::debug!("Insert coord.");
return Decision::Insert;
}
if pipe == straight_breaking_pipe {
log::debug!("Break pipe.");
return Decision::Break;
}
if !pipe.is_turn() {
log::debug!("Straight pipe, just continue.");
return Decision::Continue;
}
log::debug!("Previous, current pipes: {:?}, {:?}", prev_turn, pipe);
match (prev_turn, pipe) {
(None, _) => {
log::debug!("Update previous turning pipe.");
Decision::UpdatePrevTurn
}
(Some(a), b) => {
if _counter_turning_pipes(a, b) {
log::debug!("Counter turning pipes, break.");
Decision::Break
} else {
log::debug!("Not-counter turning pipes, continue.");
Decision::UpdatePrevTurn
}
}
}
}
fn get_maxes<T>(map: &HashMap<Coord, T>) -> (i32, i32) {
let max_r = map.keys().map(|c| c.r).max().unwrap();
let max_c = map.keys().map(|c| c.c).max().unwrap();
(max_r, max_c)
}
fn collect_nodes_visible_from_outside(
map: &HashMap<Coord, Pipe>,
graph_nodes: &HashMap<Coord, NodeIndex>,
) -> HashSet<Coord> {
let mut outsides = HashSet::new();
let (max_r, max_c) = get_maxes(map);
for r in 0..=max_r {
let mut prev_turn: Option<Pipe> = None;
for c in 0..=max_c {
let coord = Coord { r, c };
let pipe = map.get(&coord).unwrap();
log::debug!("Checking coord {:?} with character {:?}", coord, pipe);
match collection_decision(&coord, pipe, graph_nodes, &prev_turn, &Pipe::V) {
Decision::Continue => {}
Decision::Break => break,
Decision::Insert => {
outsides.insert(coord);
}
Decision::UpdatePrevTurn => {
prev_turn = Some(*pipe);
}
}
}
prev_turn = None;
for c in (0..=max_c).rev() {
let coord = Coord { r, c };
let pipe = map.get(&coord).unwrap();
match collection_decision(&coord, pipe, graph_nodes, &prev_turn, &Pipe::V) {
Decision::Continue => {}
Decision::Break => break,
Decision::Insert => {
outsides.insert(coord);
}
Decision::UpdatePrevTurn => {
prev_turn = Some(*pipe);
}
}
}
}
for c in 0..=max_c {
let mut prev_turn: Option<Pipe> = None;
for r in 0..=max_r {
let coord = Coord { r, c };
let pipe = map.get(&coord).unwrap();
match collection_decision(&coord, pipe, graph_nodes, &prev_turn, &Pipe::H) {
Decision::Continue => {}
Decision::Break => break,
Decision::Insert => {
outsides.insert(coord);
}
Decision::UpdatePrevTurn => {
prev_turn = Some(*pipe);
}
}
}
prev_turn = None;
for r in (0..=max_r).rev() {
let coord = Coord { r, c };
let pipe = map.get(&coord).unwrap();
match collection_decision(&coord, pipe, graph_nodes, &prev_turn, &Pipe::H) {
Decision::Continue => {}
Decision::Break => break,
Decision::Insert => {
outsides.insert(coord);
}
Decision::UpdatePrevTurn => {
prev_turn = Some(*pipe);
}
}
}
}
outsides
}
fn make_opposite_graphs(
input_map: &HashMap<Coord, Pipe>,
graph1_nodes: &HashMap<Coord, NodeIndex>,
) -> (UnGraph<Coord, ()>, HashMap<Coord, NodeIndex>) {
let mut outs_graph = UnGraph::new_undirected();
let outs_nodes = input_map
.iter()
.filter(|(c, _)| !graph1_nodes.contains_key(c))
.map(|(c, _)| (*c, outs_graph.add_node(*c)))
.collect::<HashMap<Coord, NodeIndex>>();
let (max_r, max_c) = get_maxes(&outs_nodes);
for (coord, idx) in outs_nodes.iter() {
for (dr, dc) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
let neighbor_coord = Coord {
r: coord.r + dr,
c: coord.c + dc,
};
if (neighbor_coord.r < 0)
| (neighbor_coord.c < 0)
| (neighbor_coord.r > max_r)
| (neighbor_coord.c > max_c)
{
continue;
} else if !graph1_nodes.contains_key(&neighbor_coord) {
log::debug!("Making edge: {:?} --> {:?}", coord, neighbor_coord);
let neighbor_idx = outs_nodes.get(&neighbor_coord).unwrap();
if !outs_graph.contains_edge(*idx, *neighbor_idx) {
outs_graph.add_edge(*idx, *neighbor_idx, ());
}
}
}
}
(outs_graph, outs_nodes)
}
fn log_map(map: &HashMap<Coord, Pipe>, nodes: &HashMap<Coord, NodeIndex>) {
let (max_r, max_c) = get_maxes(map);
let mut row_strs = Vec::new();
for r in 0..=max_r {
let mut row = Vec::new();
for c in 0..=max_c {
let coord = Coord { r, c };
if nodes.contains_key(&coord) {
row.push(".")
} else {
row.push(" ")
}
}
row_strs.push(row.join(" ").to_string())
}
let graph_str = row_strs.join("\n");
log::info!("MAZE:\n{}", graph_str);
}
// !NOTE: Currently fails the fourth example.
// !Seems like there is a bug in the building of the starting map.
// (Answer with current algorithm, 404, is too low.)
pub fn puzzle_2(input: &str) -> Result<usize, PuzzleErr> {
// 1. Create graph from puzzle 1.
let maze = parse_input(input)?;
log_map(&maze.map, &maze.node_indices);
// 2. Create a graph all other nodes not in graph 1.
let (mut outside_graph, node_idx) = make_opposite_graphs(&maze.map, &maze.node_indices);
// use petgraph::dot::{Config, Dot};
// log::debug!(
// "\n{:?}",
// Dot::with_config(&outside_graph, &[Config::EdgeNoLabel])
// );
// 3. Collect nodes directly "visible" from the outside.
let outsides = collect_nodes_visible_from_outside(&maze.map, &maze.node_indices);
log::debug!("Outsides:");
for o in outsides.iter() {
log::debug!(" {:?}", o);
}
// 4. Remove nodes connected to the nodes "visible" from the outside.
let mut outsides_idx = HashSet::new();
for out in outsides.iter() {
outsides_idx.insert(node_idx.get(out).unwrap());
}
let mut dfs_space = DfsSpace::default();
for n_i in node_idx.values() {
for out in outsides.iter() {
let out_i = *node_idx.get(out).unwrap();
if algo::has_path_connecting(&outside_graph, *n_i, out_i, Some(&mut dfs_space)) {
outsides_idx.insert(n_i);
break;
}
}
}
outside_graph.retain_nodes(|_, n| !outsides_idx.contains(&n));
// Solution is the number of remaining nodes.
Ok(outside_graph.node_count())
}
pub fn main(data_dir: &str) {
println!("Day 10: Pipe Maze");
let data = load(data_dir, 10, None);
// Puzzle 1.
let answer_1 = puzzle_1(&data);
match answer_1 {
Ok(x) => println!(" Puzzle 1: {}", x),
Err(e) => panic!("No solution to puzzle 1: {}.", e),
}
assert_eq!(answer_1, Ok(6867));
// Puzzle 2.
// let answer_2 = puzzle_2(&data);
// match answer_2 {
// Ok(x) => println!(" Puzzle 2: {}", x),
// Err(e) => panic!("No solution to puzzle 2: {}", e),
// }
// 404 (too low)
// assert_eq!(answer_2, Ok(933))
}