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 mut small = 0;
77 for (key, value) in &indices {
78 if key.as_bytes()[0].is_ascii_lowercase() {
79 small |= 1 << value;
80 }
81 }
82
83 Input { small, edges }
84}
85
86/// Explore the cave system visiting all small caves only once.
87pub fn part1(input: &Input) -> u32 {
88 explore(input, false)
89}
90
91/// Explore the cave system visiting a single small cave twice and the other small caves only once.
92pub fn part2(input: &Input) -> u32 {
93 explore(input, true)
94}
95
96/// Convenience method to create initial state.
97fn explore(input: &Input, twice: bool) -> u32 {
98 // Calculate the needed size of the cache as the product of:
99 // * 2 states for boolean "twice".
100 // * n states for the number of caves including start and end.
101 // * 2⁽ⁿ⁻²⁾ states for the possible visited combinations, not including start and end cave.
102 let size = 2 * input.edges.len() * (1 << (input.edges.len() - 2));
103 let mut cache = vec![0; size];
104
105 let state = State { from: START, visited: 0, twice };
106 paths(input, &state, &mut cache)
107}
108
109/// Core recursive DFS logic.
110///
111/// First we check if we have either reached the `end` cave or seen this state before,
112/// returning early in either case with the respective result.
113///
114/// Next we use bit manipulation to quickly iterate through the caves connected to our current
115/// location. The [`trailing_zeros`] method returns the next set bit. This intrinsic compiles to
116/// a single machine code instruction on x86 and ARM and is blazing fast. We remove visited caves
117/// using a `^` XOR instruction.
118///
119/// The nuance is reusing the same code for both part one and part two. First we check if we can visit
120/// a cave using the rules for part one. If not, then we also check if the `twice` variable is
121/// still `true`. This variable allows a single second visit to a small cave. The expression
122/// `once && twice` sets this value to `false` whenever we need to use it to visit a small cave.
123///
124/// [`trailing_zeros`]: u32::trailing_zeros
125fn paths(input: &Input, state: &State, cache: &mut [u32]) -> u32 {
126 let State { from, visited, twice } = *state;
127
128 // Calculate index by converting "twice" to either 1 or 0, then multiplying "from" by 2
129 // (the cardinality of "twice") and "visited" by "edges.len()".
130 // Subtle nuance, by not multiplying "visited" by 2 and also dividing by 2 we ignore the
131 // two least significant bits for start and end cave, as these will always be 0 and 1
132 // respectively.
133 let index = twice as usize + 2 * from + (input.edges.len() * (visited as usize / 2));
134 if cache[index] > 0 {
135 return cache[index];
136 }
137
138 let mut caves = input.edges[from];
139 let mut total = 0;
140 let end = 1 << END;
141
142 if caves & end != 0 {
143 caves ^= end;
144 total += 1;
145 }
146
147 for to in caves.biterator() {
148 let mask = 1 << to;
149 let once = input.small & mask == 0 || visited & mask == 0;
150
151 if once || twice {
152 let next = State { from: to, visited: visited | mask, twice: once && twice };
153 total += paths(input, &next, cache);
154 }
155 }
156
157 cache[index] = total;
158 total
159}