-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_23.rs
179 lines (149 loc) Β· 4.55 KB
/
day_23.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
use std::{
collections::{HashMap, HashSet},
convert::identity,
};
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};
solution!("A Long Walk", 23);
type Pos = Vec2<usize>;
fn part_a(input: &str) -> Answer {
solve_a(&parse(input), &mut HashSet::new(), vector!(1, 0), 0).into()
}
fn part_b(input: &str) -> Answer {
solve_b(&parse(input)).into()
}
// Simple DFS to find the longest path for part A
fn solve_a(map: &Matrix<char>, visited: &mut HashSet<Pos>, pos: Pos, idx: u32) -> u32 {
if pos == map.size() - vector!(2, 1) {
return idx;
}
let mut longest = 0;
for dir in Direction::ALL.iter() {
let Some(new_pos) = dir.try_advance(pos) else {
continue;
};
let next = map[new_pos];
if next == '#' || !dir_matches(*dir, next) && next != '.' || !visited.insert(pos) {
continue;
}
longest = longest.max(solve_a(map, visited, new_pos, idx + 1));
visited.remove(&pos);
}
longest
}
// Convert the map into a graph and collapse it to find the longest path for part B
fn solve_b(map: &Matrix<char>) -> u32 {
// == Build graph ==
let mut graph = HashMap::new();
let mut add_edge = |a: Pos, b: Pos| {
graph.entry(a).or_insert_with(HashSet::new).insert((b, 1));
graph.entry(b).or_insert_with(HashSet::new).insert((a, 1));
};
for y in 0..map.size().y() {
for x in 0..map.size().x() {
let pos = vector!(x, y);
if map[pos] == '#' {
continue;
}
for new_pos in Direction::ALL.iter().filter_map(|dir| dir.try_advance(pos)) {
if map.contains(new_pos) && map[new_pos] != '#' {
add_edge(pos, new_pos);
}
}
}
}
// == Collapse graph ==
let mut dirty = true;
while dirty {
dirty = false;
for key in graph.keys().copied().collect::<Vec<_>>() {
if graph[&key].len() != 2 {
continue;
}
let ((a, a_score), (b, b_score)) = {
let mut iter = graph[&key].iter();
(*iter.next().unwrap(), *iter.next().unwrap())
};
let a_graph = graph.get_mut(&a).unwrap();
a_graph.retain(|(pos, _)| *pos != key);
a_graph.insert((b, a_score + b_score));
let b_graph = graph.get_mut(&b).unwrap();
b_graph.retain(|(pos, _)| *pos != key);
b_graph.insert((a, a_score + b_score));
graph.remove(&key);
dirty = true;
}
}
// == Find longest path ==
let mut queue = Vec::new();
let mut visited = HashSet::new();
let mut max = 0;
queue.push((vector!(1, 0), Some(0)));
while let Some((pos, distance)) = queue.pop() {
let Some(distance) = distance else {
visited.remove(&pos);
continue;
};
if pos == map.size() - vector!(2, 1) {
max = max.max(distance);
continue;
}
if !visited.insert(pos) {
continue;
}
queue.push((pos, None));
for (pos, dist) in &graph[&pos] {
queue.push((*pos, Some(distance + dist)));
}
}
max
}
fn parse(input: &str) -> Matrix<char> {
Matrix::new_chars(input, identity)
}
fn dir_matches(dir: Direction, chr: char) -> bool {
chr == match dir {
Direction::Up => '^',
Direction::Down => 'v',
Direction::Left => '<',
Direction::Right => '>',
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 94.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 154.into());
}
}