-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2021_12.rs
236 lines (198 loc) · 5.91 KB
/
solution_2021_12.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
use std::str::FromStr;
use advent_of_code_common::parsing::{Error, parse_lines_to_nonempty, split_into_two_strings};
use itertools::Itertools;
use nonempty::{NonEmpty, nonempty};
use pathfinding::prelude::bfs_reach;
const DATA: &str = include_str!("../../resources/12.txt");
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
enum Vertex {
Start,
End,
Big { name: String },
Small { name: String },
}
impl FromStr for Vertex {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = if s == "start" {
Vertex::Start
} else if s == "end" {
Vertex::End
} else if s.to_lowercase() == s {
Vertex::Small {
name: s.to_string(),
}
} else {
Vertex::Big {
name: s.to_string(),
}
};
Ok(result)
}
}
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
struct Edge {
a: Vertex,
b: Vertex,
}
impl Edge {
fn has(&self, what: &Vertex) -> Option<&Vertex> {
if &self.a == what {
Some(&self.b)
} else if &self.b == what {
Some(&self.a)
} else {
None
}
}
}
impl FromStr for Edge {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (a_str, b_str) = split_into_two_strings(s, "-")?;
let a = a_str.parse()?;
let b = b_str.parse()?;
Ok(Edge { a, b })
}
}
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
struct Path {
vertices: NonEmpty<Vertex>,
}
type SmallVertexValidContinuationF = fn(&Path, &Vertex) -> bool;
impl Path {
fn start() -> Path {
Path {
vertices: nonempty![Vertex::Start],
}
}
fn last(&self) -> &Vertex {
self.vertices.last()
}
// visit small caves at most once, and can visit big caves any number of times
fn valid_continuation(
&self,
v: &Vertex,
continuation_f: SmallVertexValidContinuationF,
) -> bool {
if self.last() == &Vertex::End {
false
} else {
match v {
Vertex::Start => false,
Vertex::Big { .. } => true,
_ => continuation_f(self, v),
}
}
}
// Should visit small caves at most once, and can visit big caves any number of
// times
fn small_vertex_is_valid_continuation_1(&self, v: &Vertex) -> bool {
!self.vertices.contains(v)
}
fn is_small_cave_already_visited_twice(&self) -> bool {
let all_small: Vec<_> = self
.vertices
.iter()
.filter(|p| matches!(p, Vertex::Small { .. }))
.collect();
let unique_small_count = all_small.iter().unique().count();
all_small.len() != unique_small_count
}
// Specifically, big caves can be visited any number of times, a single small
// cave can be visited at most twice, and the remaining small caves can be
// visited at most once.
fn small_vertex_is_valid_continuation_2(&self, v: &Vertex) -> bool {
self.small_vertex_is_valid_continuation_1(v) || !self.is_small_cave_already_visited_twice()
}
fn with_continuation(&self, v: &Vertex) -> Path {
let mut vertices = self.vertices.clone();
vertices.push(v.clone());
Path { vertices }
}
}
struct Graph {
edges: NonEmpty<Edge>,
}
impl Graph {
fn neighbours(&self, vertex: &Vertex) -> Vec<&Vertex> {
self.edges.iter().filter_map(|e| e.has(vertex)).collect()
}
fn successors(&self, path: &Path, continuation_f: SmallVertexValidContinuationF) -> Vec<Path> {
let neighbours_for_last = self.neighbours(path.last());
let valid_continuations: Vec<&&Vertex> = neighbours_for_last
.iter()
.filter(|v| path.valid_continuation(v, continuation_f))
.collect();
valid_continuations
.iter()
.map(|v| path.with_continuation(v))
.collect()
}
}
impl FromStr for Graph {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let edges: NonEmpty<Edge> = parse_lines_to_nonempty(input)?;
Ok(Graph { edges })
}
}
fn solve(input: &str, continuation_f: SmallVertexValidContinuationF) -> Result<usize, Error> {
let graph: Graph = input.parse()?;
let result = bfs_reach(Path::start(), |path| graph.successors(path, continuation_f))
.filter(|p| p.last() == &Vertex::End)
.count();
Ok(result)
}
fn solve_1(input: &str) -> Result<usize, Error> {
solve(input, Path::small_vertex_is_valid_continuation_1)
}
fn solve_2(input: &str) -> Result<usize, Error> {
solve(input, Path::small_vertex_is_valid_continuation_2)
}
fn main() {
let result_1 = solve_1(DATA);
println!("Part 1: {result_1:?}");
let result_2 = solve_2(DATA);
println!("Part 2: {result_2:?}");
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_DATA_1: &str = include_str!("../../resources/12-test-1.txt");
const TEST_DATA_2: &str = include_str!("../../resources/12-test-2.txt");
const TEST_DATA_3: &str = include_str!("../../resources/12-test-3.txt");
#[test]
fn test_solve_1_test_1() {
assert_eq!(solve_1(TEST_DATA_1), Ok(10));
}
#[test]
fn test_solve_1_test_2() {
assert_eq!(solve_1(TEST_DATA_2), Ok(19));
}
#[test]
fn test_solve_1_test_3() {
assert_eq!(solve_1(TEST_DATA_3), Ok(226));
}
#[test]
fn test_solve_1_real() {
assert_eq!(solve_1(DATA), Ok(3576));
}
#[test]
fn test_solve_2_test_1() {
assert_eq!(solve_2(TEST_DATA_1), Ok(36));
}
#[test]
fn test_solve_2_test_2() {
assert_eq!(solve_2(TEST_DATA_2), Ok(103));
}
#[test]
fn test_solve_2_test_3() {
assert_eq!(solve_2(TEST_DATA_3), Ok(3509));
}
#[test]
#[ignore]
fn test_solve_2_real() {
assert_eq!(solve_2(DATA), Ok(84271));
}
}