pub fn y23d08(input: &str, part: u32) -> u64
Expand description
The solution for the day eight challenge.
We take the input as a string and the part we’re solving as an integer.
The solution otherwise remains the same, we just use a single starting room
in our vector of starting rooms if we’re in part one (the room AAA
) and
otherwise all of the rooms that end in A
. We start by parsing the input
and build a std::collections::HashMap
of the rooms and their left and
right connections. Then, for each of our starting rooms we loop until we’re
in the ending room (in part one the room ZZZ
and in part two the room
ending in Z
). If we’re not in the ending room then we take the next
instruction and change to the next room, keeping track of how many steps
we’ve taken. Once we’ve computed the steps for all of the starting rooms we
LCM them together to get our final answer.
§Example
// probably read this from the input file...
let input = "LRR\n\n(AAA) = (ZZZ, XXX)";
assert_eq!(y23d08(input, 1), 1);
assert_eq!(y23d08(input, 2), 1);