-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_12.rs
165 lines (140 loc) Β· 3.58 KB
/
day_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
use common::{solution, Answer};
use hashbrown::{HashMap, HashSet};
use petgraph::graph::{NodeIndex, UnGraph};
solution!("Passage Pathing", 12);
fn part_a(input: &str) -> Answer {
let graph = parse(input);
fn paths(graph: &ParseResult, at: NodeIndex, mut visited: HashSet<NodeIndex>) -> usize {
if at == graph.end {
return 1;
}
if !visited.insert(at) && graph.graph[at].cave_type != Type::Big {
return 0;
}
graph
.graph
.neighbors(at)
.map(|child| paths(graph, child, visited.clone()))
.sum()
}
paths(&graph, graph.start, HashSet::new()).into()
}
fn part_b(input: &str) -> Answer {
let graph = parse(input);
fn paths(
graph: &ParseResult,
at: NodeIndex,
mut visited: HashSet<NodeIndex>,
mut small_twice: bool,
) -> usize {
if at == graph.end {
return 1;
}
let cave = graph.graph[at].cave_type;
if !visited.insert(at) && cave != Type::Big {
if !small_twice && cave == Type::Small {
small_twice = true;
} else {
return 0;
}
}
graph
.graph
.neighbors(at)
.map(|child| paths(graph, child, visited.clone(), small_twice))
.sum()
}
paths(&graph, graph.start, HashSet::new(), false).into()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Type {
Small,
Big,
Root,
}
struct Node<'a> {
name: &'a str,
cave_type: Type,
}
struct ParseResult<'a> {
graph: UnGraph<Node<'a>, ()>,
start: NodeIndex,
end: NodeIndex,
}
fn parse(input: &str) -> ParseResult {
let mut graph = UnGraph::new_undirected();
let mut nodes = HashMap::new();
fn make_node(name: &str) -> Node {
Node {
name,
cave_type: if name == "start" || name == "end" {
Type::Root
} else if name.chars().next().unwrap().is_uppercase() {
Type::Big
} else {
Type::Small
},
}
}
fn get_node<'a>(
nodes: &mut HashMap<&'a str, NodeIndex>,
graph: &mut UnGraph<Node<'a>, ()>,
name: &'a str,
) -> NodeIndex {
*nodes
.entry(name)
.or_insert_with(|| graph.add_node(make_node(name)))
}
for line in input.lines() {
let (from, to) = line.split_once('-').unwrap();
let from = get_node(&mut nodes, &mut graph, from);
let to = get_node(&mut nodes, &mut graph, to);
graph.add_edge(from, to, ());
}
fn find<'a>(graph: &'a UnGraph<Node<'a>, ()>, name: &str) -> NodeIndex {
graph
.node_indices()
.find(|i| graph[*i].name == name)
.unwrap()
}
ParseResult {
start: find(&graph, "start"),
end: find(&graph, "end"),
graph,
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
start-A
start-b
A-c
A-b
b-d
A-end
b-end
"};
const CASE_2: &str = indoc! {"
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 10.into());
assert_eq!(super::part_a(CASE_2), 19.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 36.into());
assert_eq!(super::part_b(CASE_2), 103.into());
}
}