fn paths(input: &Input, state: &State, cache: &mut [u32]) -> u32
Expand description
Core recursive DFS logic.
First we check if we have either reached the end
cave or seen this state before,
returning early in either case with the respective result.
Next we use bit manipulation to quickly iterate through the caves connnected to our current
location. The trailing_zeros
method returns the next set bit. This instrinsic compiles to
a single machine code instruction on x86 and ARM and is blazing fast. We remove visited caves
using a ^
XOR instruction.
The nuance is re-using the same code for both part 1 and part 2. First we check if we can visit
a cave using the rules for part 1. If not, then we also check if the twice
variable is
still true
. This variable allows a single second visit to a small cave. The expression
once && twice
sets this value to false
whenever we need to use it to visit a small cave.