pub fn y15d14(input: &str, seconds: u32, part: u32) -> u32
Expand description
The solution for the day fourteen challenge.
As usual we accept the input as a string. Then we take how many seconds of
race to process and whether we’re doing part 1
(return the maximum
distance) or part 2
(return the maximum number of points). We start by
parsing the input and creating our Reindeer
objects to track the state
as the race progresses. Then for the desired number or race seconds we
process each second one-at-a-time. For each second we create a new max-heap
(using the venerable std::collections::BinaryHeap
to keep track of the
current maximum travelled distance. We process each reindeer and add their
distance to the heap. We then pop the heap to see what the maximum distance
is across all reindeer and then any reindeer who is currently at that
winning distance is awarded a point. To return the desired final answer we
create a new max-heap and then add each reindeer’s distance for part one or
number of obtained points for part two. Popping the heap gives us the
answer.
§Example
// probably read this from the input file...
let input = concat!(
"Santa can fly 25 km/s for 10 seconds, but ",
"then must rest for 10 seconds."
);
assert_eq!(y15d14(input, 100, 1), 1250);
assert_eq!(y15d14(input, 100, 2), 100);