pub fn y23d04(input: &str, part: u32) -> u32
Expand description
The solution for the day four challenge.
As usual we take the input as a string and a number for part 1
or part
2
. If we’re in part 2
then we start by building a
std::collections::HashMap
which will store the amount of each card that
we have and which we initialize each card to 1
. We then parse the input
which is basically just a bunch of string splitting and number parsing to
get the winning numbers and the numbers that we have. The total number of
matches is the intersection between the two sets. Then in part 1
we
calculate 2^(number of matches - 1) which gives us the point value for
each card and add it to the sum. In part two we instead add the number of
cards that we currently have to each of the next number of matches cards.
In part 1
we can just return the sum that we calculated. In part 2
we
return the sum of the values of the cards hash (how man of each card).
§Example
// probably read this from the input file...
let input = "Card 1: 1 2 3 | 2 3\nCard 2: 1 2 | 1\nCard 3: 1 2 3 | 4";
assert_eq!(y23d04(input, 1), 3);
assert_eq!(y23d04(input, 2), 7);