pub fn y22d04(input: &str, part: u32) -> u32
Expand description
The solution for the day four challenge.
Given the input as a string and an integer depending if we want to solve
part 1
or part 2
of the challenge, we start by doing the usual split
of lines and initialization of the return sum. Then we loop through the
lines and convert them into their numerical couterparts as explained
above.
In the first part it’s just a simple check if the first range start is greater than (or equal to) the second range start and the first range end is less than (or equal to) the second range end (or vice versa).
The second part we convert the first range into an actual range (plus one on the end because ranges are not inclusive) and then loop through the numbers in the second range to see if one of them is contained in the first range (then break in case more numbers are contained which gives us too large of a number).
§Example
// probably read this from the input file...
let input = "2-3,3-4\n1-1,1-4\n";
assert_eq!(y22d04(input, 1), 1);
assert_eq!(y22d04(input, 2), 2);