-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
185 lines (166 loc) · 5.5 KB
/
main.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
use petgraph::algo::floyd_warshall;
use petgraph::{Directed, Graph};
use std::collections::{HashMap, VecDeque};
fn parse_input(input: &str) -> Vec<(String, usize, Vec<String>)> {
input
.trim()
.lines()
.map(|line| {
match sscanf::sscanf!(
line,
"Valve {String} has flow rate={usize}; {String} {String} to {String} {String}"
) {
Ok(r) => r,
Err(_) => panic!("unable to match: {}", line),
}
})
.map(|(origin, rate, _, _, _, destinations)| {
(
origin,
rate,
destinations
.split(", ")
.map(|s| s.to_string())
.collect::<Vec<String>>(),
)
})
.collect()
}
fn calc_distances(input: &[(String, usize, Vec<String>)]) -> HashMap<(String, String), usize> {
let mut graph: Graph<String, (), Directed> = Graph::new();
let graph_nodes = input
.iter()
.map(|(name, _, _)| (name.clone(), graph.add_node(name.clone())))
.collect::<HashMap<_, _>>();
for (id, _, next) in input.iter() {
for dest in next.iter() {
graph.add_edge(graph_nodes[id], graph_nodes[dest], ());
}
}
floyd_warshall(&graph, |_| 1)
.unwrap()
.iter()
.map(|((from, to), cost)| {
(
(
graph.node_weight(*from).unwrap().clone(),
graph.node_weight(*to).unwrap().clone(),
),
*cost as usize,
)
})
.collect()
}
fn extend_vec<T: Clone>(vec: &Vec<T>, el: T) -> Vec<T> {
let mut result = Vec::with_capacity(vec.len() + 1);
for it in vec {
result.push(it.clone());
};
result.push(el);
result
}
fn part1(input: &str) -> usize {
let input = parse_input(input);
let distances = calc_distances(&input);
let nodes_with_rate = input
.iter()
.filter(|(_, r, _)| r > &0)
.map(|(origin, rate, _)| (origin.clone(), *rate))
.collect::<HashMap<String, usize>>();
let minutes = 30;
let mut best = 0;
let mut queue = VecDeque::new();
let start = "AA".to_string();
queue.push_back((&start, 0, vec![], 0));
while let Some((id, minute, open, released)) = queue.pop_front() {
if released > best {
best = released;
}
for next in nodes_with_rate.keys() {
if !open.contains(&next) {
let dist = distances.get(&(id.clone(), (*next).clone())).unwrap();
if minute + dist < minutes {
let rate = nodes_with_rate.get(next).unwrap();
let released = released + rate * (minutes - dist - minute - 1);
queue.push_back((next, minute + dist + 1, extend_vec(&open, next), released));
}
}
}
}
best
}
fn part2(input: &str) -> usize {
let input = parse_input(input);
let distances = calc_distances(&input);
let nodes_with_rate = input
.iter()
.filter(|(_, r, _)| r > &0)
.map(|(origin, rate, _)| (origin.clone(), *rate))
.collect::<HashMap<String, usize>>();
let minutes = 26;
let mut best = 0;
let mut queue = VecDeque::new();
let start = "AA".to_string();
queue.push_back((&start, &start, 0, 0, vec![], 0));
let mut best_cache = HashMap::new();
while let Some((a, b, a_minute, b_minute, open, released)) = queue.pop_front() {
if released > best {
best = released;
}
let k = (a, b, a_minute, b_minute);
if let Some(&prev_best) = best_cache.get(&k) {
if prev_best >= released {
continue;
}
}
best_cache.insert(k, released);
let [a_next, b_next] = [(a, a_minute), (b, b_minute)].map(|(node, min)| {
nodes_with_rate
.keys()
.filter(|d| {
!open.contains(d)
&& distances.get(&(node.clone(), (*d).clone())).unwrap() < &(minutes - min)
})
.collect::<Vec<_>>()
});
for &a_next in a_next.iter() {
let a_rate = nodes_with_rate.get(a_next).unwrap();
let a_dist = distances.get(&(a.clone(), (*a_next).clone())).unwrap();
let a_newly_released = a_rate * (minutes - a_dist - a_minute - 1);
queue.push_back((
a_next,
b,
a_minute + a_dist + 1,
b_minute,
extend_vec(&open, a_next),
released + a_newly_released,
));
}
for &b_next in b_next.iter() {
let b_rate = nodes_with_rate.get(b_next).unwrap();
let b_dist = distances.get(&(b.clone(), (*b_next).clone())).unwrap();
let b_newly_released = b_rate * (minutes - b_dist - b_minute - 1);
queue.push_back((
a,
b_next,
a_minute,
b_minute + b_dist + 1,
extend_vec(&open, b_next),
released + b_newly_released,
));
}
}
best
}
fn main() {
println!("Part 1: {:?}", part1(include_str!("input.txt")));
println!("Part 2: {:?}", part2(include_str!("input.txt")));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test.txt")), 1651);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test.txt")), 1707);
}