Function aoc::y23d06::y23d06

source ·
pub fn y23d06(input: &str, part: u32) -> u64
Expand description

The solution for the day six challenge.

As usual, we take the input as a string and an integer for whether we’re solving part 1 or part 2. The difference in part just changes how we parse the input. Either we have an array times and distances or we concatenate the input to create a single race (which we then turn into a vector of length one so that we can re-use the solution from part one without any other modifications). Once we have our input arrays we zip them together to create an array that matches the times and distances for each race and then proceed to do the calculation as described in the prompt. We start from 1 (holding the boat for 0 milliseconds causes it to go nowhere) until the length of the race (minus one as just like not holding it at all holding it for the entire race causes it to go nowhere). With the remaining time and speed we calculate how far the boat will go and if it’s greater than the record for the race then we increment our counter for that race. To get our final result we just multiply all of our counters together (in the actual implementation we multiply them as we go).

§Example

// probably read this from the input file...
let input = "Time: 10 40\nDistance: 10 100";
assert_eq!(y23d06(input, 1), 245);
assert_eq!(y23d06(input, 2), 1021);