Skip to main content

aoc/year2022/
day16.rs

1//! # Proboscidea Volcanium
2//!
3//! ## Parsing
4//!
5//! First we simplify the graph formed by the valves. With the exception of `AA` there's no need to
6//! stop at any zero valve, so we're only interested in the distance between non-zero valves.
7//! This significantly reduces the complexity of the solution space as there are only around
8//! 15 non-zero valves versus around 60 valves total.
9//!
10//! For each valve we find the distance to its immediate non-zero neighbors. Then we use the
11//! [Floyd Warshall algorithm](https://en.wikipedia.org/wiki/Floyd-Warshall_algorithm) to
12//! find the distance between any two non-zero valves, storing this information in an
13//! [adjacency matrix](https://en.wikipedia.org/wiki/Adjacency_matrix) for fast lookup.
14//!
15//! ## Part One
16//!
17//! The approach is [branch and bound](https://en.wikipedia.org/wiki/Branch_and_bound) enumerating
18//! every possible combination combined with a heuristic to prune those combinations in order to
19//! achieve a reasonable running time.
20//!
21//! The heuristic assumes that we can visit all remaining valves in descending order of flow,
22//! taking only the minimum possible time to reach each valve. As this will always be better
23//! than the actual maximum possible we can immediately prune any branch that would still be less
24//! than the current high score.
25//!
26//! ## Part Two
27//!
28//! Part two uses an ingenious approach from [@korreman](https://github.com/korreman/aoc2022).
29//!
30//! First calculate the maximum value for any possible combination of valves reachable in
31//! 26 minutes by a single entity. Then calculate a second score from the remaining unopened
32//! valves.
33//!
34//! The neat part is using this second score as the heuristic threshold for a search over all
35//! possible valve combinations. This works as the sum of the first two searches provides a
36//! minimum baseline. If a branch can't do better then it can be pruned.
37//!
38//! Then we check every possible pair formed by those values, considering only the pairs
39//! where the sets of valves are [disjoint](https://en.wikipedia.org/wiki/Disjoint_sets),
40//! which is when you and the elephant have visited different sets of valves.
41use std::cmp::Reverse;
42
43use crate::util::bitset::*;
44use crate::util::hash::*;
45use crate::util::parse::*;
46
47/// Simplified graph of valves. Valves are stored in descending order of flow so the valve at
48/// index 0 has the highest flow, valve at index 1 the second highest and so on.
49/// This descending order is used by the heuristic to prune branches.
50///
51/// * `size` Number of non-zero valves plus 1 for `AA`.
52/// * `all_valves` Bitmask with a `1` for each initial unopened non-zero valve. For example, if
53///   there are 5 valves this would be binary `11111`.
54/// * `flow` Stores the flow for each valve.
55/// * `distance` Adjacency matrix of distances between each pair of valves.
56pub struct Input {
57    size: usize,
58    aa: usize,
59    all_valves: usize,
60    flow: Vec<u32>,
61    distance: Vec<u32>,
62    closest: Vec<u32>,
63}
64
65/// State of a single exploration path through the valves.
66///
67/// * `todo` Binary mask of unopened valves. For example, if there are 3 unopened valves left this
68///   could look like `11100`.
69/// * `from` Index of current valve.
70/// * `time` The *remaining* time left.
71/// * `pressure` Total pressure released from all opened valves including future extrapolated flow.
72struct State {
73    todo: usize,
74    from: usize,
75    time: u32,
76    pressure: u32,
77}
78
79/// Intermediate struct for parsing only.
80struct Valve<'a> {
81    name: &'a str,
82    flow: u32,
83    edges: Vec<&'a str>,
84}
85
86impl Valve<'_> {
87    /// We're only interested in uppercase valve names and digits for the flow.
88    fn parse(line: &str) -> Valve<'_> {
89        let mut tokens = line
90            .split(|c: char| !c.is_ascii_uppercase() && !c.is_ascii_digit())
91            .filter(|s| !s.is_empty())
92            .skip(1);
93        let name = tokens.next().unwrap();
94        let flow = tokens.next().unwrap().unsigned();
95        Valve { name, flow, edges: tokens.collect() }
96    }
97}
98
99pub fn parse(input: &str) -> Input {
100    // Sort valves in descending order of flow then ascending alphabetical order of names.
101    // This places all non-zero valves at the start followed immediately by valve `AA`.
102    let mut valves: Vec<_> = input.lines().map(Valve::parse).collect();
103    valves.sort_unstable_by_key(|v| (Reverse(v.flow), v.name));
104
105    // We only care about non-zero valves with the exception of `AA`.
106    let size = valves.iter().filter(|v| v.flow > 0).count() + 1;
107    let mut distance = vec![u32::MAX; size * size];
108
109    // Eliminate zero valves. Assumes that zero valves are "tunnels" with each linking 2 other
110    // valves. For all non-zero valves follows the tunnels to find the distance to each
111    // immediate neighbor.
112    let indices: FastMap<_, _> = valves.iter().enumerate().map(|(i, v)| (v.name, i)).collect();
113
114    for (from, valve) in valves.iter().enumerate().take(size) {
115        // Distance to ourself is zero.
116        distance[from * size + from] = 0;
117
118        // Follow "tunnels" of zero valves to our non-zero neighbors.
119        for edge in &valve.edges {
120            let mut prev = valve.name;
121            let mut to = indices[edge];
122            let mut total = 1;
123
124            while to >= size {
125                let next = valves[to].edges.iter().find(|&&e| e != prev).unwrap();
126                prev = valves[to].name;
127                to = indices[next];
128                total += 1;
129            }
130
131            distance[from * size + to] = total;
132        }
133    }
134
135    // Floyd-Warshall algorithm to find the pairwise distance between any two valves.
136    for k in 0..size {
137        for i in 0..size {
138            for j in 0..size {
139                let candidate = distance[i * size + k].saturating_add(distance[k * size + j]);
140                distance[i * size + j] = distance[i * size + j].min(candidate);
141            }
142        }
143    }
144
145    // Add 1 minute to each distance to include the time needed to open a valve.
146    distance.iter_mut().for_each(|d| *d += 1);
147    // Index of AA is one less than size.
148    let aa = size - 1;
149    // Binary mask of all initial unopened valves not including AA.
150    let all_valves = (1 << aa) - 1;
151    // Extract flow information.
152    let flow = valves.iter().take(size).map(|v| v.flow).collect();
153    // Closest neighbor to each valve.
154    let closest = distance
155        .chunks_exact(size)
156        .map(|chunk| *chunk.iter().filter(|&&d| d > 1).min().unwrap())
157        .collect();
158
159    // Compact representation of tunnels and valves.
160    Input { size, aa, all_valves, flow, distance, closest }
161}
162
163/// Explore the tunnels, finding the highest possible score for a single entity.
164pub fn part1(input: &Input) -> u32 {
165    let mut score = 0;
166    // Return the current high score for the heuristic.
167    let mut high_score = |_, pressure| {
168        score = score.max(pressure);
169        score
170    };
171
172    let start = State { todo: input.all_valves, from: input.aa, time: 30, pressure: 0 };
173    explore(input, &start, &mut high_score);
174
175    score
176}
177
178/// Return the maximum possible score from two entities exploring the tunnels simultaneously.
179pub fn part2(input: &Input) -> u32 {
180    // Step 1
181    // Find both the highest possible score and the remaining unopened valves from you
182    // exploring the tunnels.
183    let mut you = 0;
184    let mut remaining = 0;
185    // Keep track of the unopened valves associated with the high score.
186    let mut high_score = |todo, pressure| {
187        if pressure > you {
188            you = pressure;
189            remaining = todo;
190        }
191        you
192    };
193
194    let first = State { todo: input.all_valves, from: input.aa, time: 26, pressure: 0 };
195    explore(input, &first, &mut high_score);
196
197    // Step 2
198    // Find the highest possible score when only allowing the unopened valves from the
199    // previous run. This will set a minimum baseline score for the heuristic.
200    let mut elephant = 0;
201    let mut high_score = |_, pressure| {
202        elephant = elephant.max(pressure);
203        elephant
204    };
205
206    let second = State { todo: remaining, from: input.aa, time: 26, pressure: 0 };
207    explore(input, &second, &mut high_score);
208
209    // Step 3
210    // Explore a third time allowing only scores that are higher than the previous minimum.
211    // Instead of a single score, store the high score for each of the `2ⁱ` possible combinations
212    // of valves. The index of the score is the bitmask of the *opened* valves.
213    let mut score = vec![0; input.all_valves + 1];
214    let mut high_score = |todo: usize, pressure| {
215        let done = input.all_valves ^ todo;
216        score[done] = score[done].max(pressure);
217        // Always return the elephant value from step 2 for the heuristic.
218        elephant
219    };
220
221    let third = State { todo: input.all_valves, from: input.aa, time: 26, pressure: 0 };
222    explore(input, &third, &mut high_score);
223
224    // Combine the score using the disjoint sets approach. As no valve can be opened twice
225    // only consider scores where there is no overlap by using a bitwise AND.
226    let mut result = you + elephant;
227
228    // Find valid non-zero results then sort in order to check combinations faster.
229    let mut candidates: Vec<_> = score.into_iter().enumerate().filter(|&(_, s)| s > 0).collect();
230    candidates.sort_unstable_by_key(|t| t.1);
231
232    for i in (1..candidates.len()).rev() {
233        let (mask1, you) = candidates[i];
234
235        // Since results are sorted, all subsequent scores are lower than this one.
236        // If the maximum possible sum from remaining scores is lower than the current result
237        // then we're done.
238        if you * 2 <= result {
239            break;
240        }
241
242        // Find the best result where the two sets of valves are disjoint.
243        if let Some((_, elephant)) = candidates[..i].iter().rfind(|(mask2, _)| mask1 & mask2 == 0) {
244            result = result.max(you + elephant);
245        }
246    }
247
248    result
249}
250
251fn explore(input: &Input, state: &State, high_score: &mut impl FnMut(usize, u32) -> u32) {
252    let State { todo, from, time, pressure } = *state;
253    let score = high_score(todo, pressure);
254
255    // Stores the set of unopened valves in a single integer as a bit mask with a 1
256    // for each unopened valve. This code iterates over each valve by finding the lowest
257    // 1 bit then removing it from the set.
258    for to in todo.biterator() {
259        // Check if there's enough time to reach the valve.
260        let needed = input.distance[from * input.size + to];
261        if needed >= time {
262            continue;
263        }
264
265        // Calculate the total pressure released by a valve up front.
266        let todo = todo ^ (1 << to);
267        let time = time - needed;
268        let pressure = pressure + time * input.flow[to];
269
270        // Pretend that we could visit each remaining unopened valve in descending order
271        // of flow taking only the minimum possible time to reach each valve. As this is always
272        // better than the actual graph if we can't beat the high score then we can prune
273        // this branch right away.
274        let heuristic = {
275            let mut valves = todo;
276            let mut time = time;
277            let mut pressure = pressure;
278
279            // Assume that all valves have a distance of 3 or more.
280            while valves > 0 && time > 3 {
281                let to = valves.trailing_zeros() as usize;
282                valves ^= 1 << to;
283                time -= input.closest[to];
284                pressure += time * input.flow[to];
285            }
286
287            pressure
288        };
289
290        // Only explore further if it's possible to beat the high score.
291        if heuristic > score {
292            let next = State { todo, from: to, time, pressure };
293            explore(input, &next, high_score);
294        }
295    }
296}