Skip to main content

aoc/year2021/
day12.rs

1//! # Passage Pathing
2//!
3//! Our basic approach is a [DFS](https://en.wikipedia.org/wiki/Depth-first_search) through the cave
4//! system, exploring all possible permutations of the paths and finishing whenever we reach
5//! the `end` cave.
6//!
7//! To speed things up, 2 strategies are used, one high-level and one low-level:
8//! * [Memoization](https://en.wikipedia.org/wiki/Memoization) (or caching) of the possible paths
9//!   from each position, taking into account previously visited caves is the high-level strategy
10//!   to reuse work and save time.
11//! * [Bit Manipulation](https://en.wikipedia.org/wiki/Bit_manipulation) to store both the graph of
12//!   cave connections as an [adjacency matrix](https://en.wikipedia.org/wiki/Adjacency_matrix)
13//!   and the list of visited caves compressed into a single `u32` is the low-level strategy to
14//!   quickly and efficiently store the small cardinality set of caves.
15use crate::util::bitset::*;
16use crate::util::hash::*;
17use crate::util::iter::*;
18
19const START: usize = 0;
20const END: usize = 1;
21
22pub struct Input {
23    small: u32,
24    edges: Vec<u32>,
25}
26
27struct State {
28    from: usize,
29    visited: u32,
30    twice: bool,
31}
32
33/// Parse the input into an adjacency matrix of edges compressed into `u32` bitfields.
34///
35/// First, each cave is assigned a unique index, with `0` reserved for the `start` cave and `1`
36/// reserved for the `end` cave. For example, the sample input caves are:
37///
38/// | start | end | A | b | c | d |
39/// | :---: | :-: | - | - | - | - |
40/// |   0   |  1  | 2 | 3 | 4 | 5 |
41///
42/// Next a `vec` of `u32` with an entry for each cave at the corresponding index is created with
43/// a bit set for each other cave reachable at `2ⁿ` where n is the cave index. The start cave
44/// can only be visited once at the beginning, so it is removed from all edges.
45/// For example, the sample start cave `vec` looks like:
46///
47/// | cave  | index | edges  |
48/// | ----- | ----- | ------ |
49/// | start | 0     |   1100 |
50/// | end   | 1     |   1100 |
51/// | A     | 2     |  11010 |
52/// | b     | 3     | 100110 |
53/// | c     | 4     |    100 |
54/// | d     | 5     |   1000 |
55///
56/// Finally, all small caves are added to a single `u32`, for example the
57/// sample data looks like `111011`.
58pub fn parse(input: &str) -> Input {
59    let tokens: Vec<_> =
60        input.split(|c: char| !c.is_ascii_alphabetic()).filter(|s| !s.is_empty()).collect();
61
62    let mut indices = FastMap::build([("start", START), ("end", END)]);
63    for token in &tokens {
64        let next = indices.len();
65        indices.entry(token).or_insert(next);
66    }
67
68    let mut edges = vec![0; indices.len()];
69    for [a, b] in tokens.iter().chunk::<2>() {
70        edges[indices[a]] |= 1 << indices[b];
71        edges[indices[b]] |= 1 << indices[a];
72    }
73    let not_start = !(1 << START);
74    edges.iter_mut().for_each(|edge| *edge &= not_start);
75
76    let small = indices
77        .iter()
78        .filter(|(key, _)| key.as_bytes()[0].is_ascii_lowercase())
79        .fold(0, |small, (_, index)| small | (1 << index));
80
81    Input { small, edges }
82}
83
84/// Explore the cave system visiting all small caves only once.
85pub fn part1(input: &Input) -> u32 {
86    explore(input, false)
87}
88
89/// Explore the cave system visiting a single small cave twice and the other small caves only once.
90pub fn part2(input: &Input) -> u32 {
91    explore(input, true)
92}
93
94/// Convenience method to create initial state.
95fn explore(input: &Input, twice: bool) -> u32 {
96    // Calculate the needed size of the cache as the product of:
97    // * 2 states for boolean "twice".
98    // * n states for the number of caves including start and end.
99    // * 2⁽ⁿ⁻²⁾ states for the possible visited combinations, not including start and end cave.
100    let size = 2 * input.edges.len() * (1 << (input.edges.len() - 2));
101    let mut cache = vec![0; size];
102
103    let state = State { from: START, visited: 0, twice };
104    paths(input, &state, &mut cache)
105}
106
107/// Core recursive DFS logic.
108///
109/// First we check if we have either reached the `end` cave or seen this state before,
110/// returning early in either case with the respective result.
111///
112/// Next we use bit manipulation to quickly iterate through the caves connected to our current
113/// location. The [`trailing_zeros`] method returns the next set bit. This intrinsic compiles to
114/// a single machine code instruction on x86 and ARM and is blazing fast. We remove visited caves
115/// using a `^` XOR instruction.
116///
117/// The nuance is reusing the same code for both part one and part two. First we check if we can visit
118/// a cave using the rules for part one. If not, then we also check if the `twice` variable is
119/// still `true`. This variable allows a single second visit to a small cave. The expression
120/// `once && twice` sets this value to `false` whenever we need to use it to visit a small cave.
121///
122/// [`trailing_zeros`]: u32::trailing_zeros
123fn paths(input: &Input, state: &State, cache: &mut [u32]) -> u32 {
124    let State { from, visited, twice } = *state;
125
126    // Calculate index by converting "twice" to either 1 or 0, then multiplying "from" by 2
127    // (the cardinality of "twice") and "visited" by "edges.len()".
128    // Subtle nuance, by not multiplying "visited" by 2 and also dividing by 2 we ignore the
129    // two least significant bits for start and end cave, as these will always be 0 and 1
130    // respectively.
131    let index = twice as usize + 2 * from + (input.edges.len() * (visited as usize / 2));
132    if cache[index] > 0 {
133        return cache[index];
134    }
135
136    let mut caves = input.edges[from];
137    let mut total = 0;
138    let end = 1 << END;
139
140    if caves & end != 0 {
141        caves ^= end;
142        total += 1;
143    }
144
145    for to in caves.biterator() {
146        let mask = 1 << to;
147        let once = input.small & mask == 0 || visited & mask == 0;
148
149        if once || twice {
150            let next = State { from: to, visited: visited | mask, twice: once && twice };
151            total += paths(input, &next, cache);
152        }
153    }
154
155    cache[index] = total;
156    total
157}