pub fn y15d15(input: &str, part: u32) -> i32
Expand description
The solution for the day fifteen challenge.
As usual we take the input as a string and a variable to determine which
part we’re computing (in part 2
we need to limit the options to cookies
that result in 500 calories). We start by processing the input and building
Ingredients
to keep track of the various qualities of the ingredients.
We then create a vector of vectors, one for each ingredient where the
sub-vector are all of the possible teaspoons of the ingredient to use to
make the cookie (0 to 100). We then take the cartesian product of those
vectors to get all of the possible recipes (both valid and invalid) and
then filter the result to only the valid options (i.e., that use 100 total
teaspoons, and in part 2
are 500 calories). With the list of valid
recipes we compute the score of each one and add it to our
std::collections::BinaryHeap
. To get the highest possible score we just
pop the heap.
§Example
// probably read this from the input file...
let input = concat!(
"Candy: capacity -1, durability 3, flavor 1, texture 1, calories 2\n",
"Chocolate: capacity 2, durability 2, flavor 1, texture 1, calories 8",
);
assert_eq!(y15d15(input, 1), 400000000);
assert_eq!(y15d15(input, 2), 125000000);