pub fn y23d10(input: &str, part: u32) -> u64
Expand description
The solution for the day ten challenge.
We take the input as a string and an integer to denote the part that we’re
solving. In part 1
we just need to get the farthest point from the start.
In part 2
we need to calculate the number of spaces enclosed by the loop
of pipes. We start by parsing the input into a
std::collections::HashMap
of points pointing to the section of pipe.
We also note down the starting point which we’ll need to calculate the
loop. We then collect the vector of points in the loop. If we’re in part
1
then we can calculate the farthest point by simply dividing the length
of the points vector by two. In part 2
we instead pull out all of the
corners to compute the area of the loop using the shoelace formula.
Importantly, we assume that the starting point is a corner, which may or
may not be the case for all inputs but is the case for all of the example
inputs as well as my puzzle input. Provided an input where that was not the
case we would first need to figure out if that start was a corner or not
and then adjust accordingly. Then we use Pick’s theorem to calculate the
number of points inside the loop (because we already have the area and the
number of points along the border).
§Example
// probably read this from the input file...
let input = concat!(
".........\n",
".S--7....\n",
".|..L--7.\n",
".|.....|.\n",
".|.F-7.|.\n",
".|.|.L-J.\n",
".L-J.....\n",
".........",
);
assert_eq!(y23d10(input, 1), 12);
assert_eq!(y23d10(input, 2), 10);