pub fn y22d03(input: &str, part: u32) -> u32
Expand description
The solution for the day three challenge. In reality calls a second
function based on the second argument (part 1
or part 2
).
For part one given the input as a string we loop through the characters in each line. If we’re in the first half we add them to the set, if we’re in the second half we check if the character is in the set (meaning that we’ve found the duplicate) and return the priority. Then break because it’s possible that the duplicate character can exist more than once which results in a total that is too large.
For the second part given the input as a string we calculate the number of elf groups (lines divided by three because the elves are in groups of three). Then for each group we loop through the characters in the third group and check if it exists in both the first and second group (this could also be loop through the first and check the second and third or loop through the second and check the first and third with the same outcome) and if so add the priority to the total.
§Example
// probably read this from the input file...
let input = "jaabbcck\njddeeffl\njgghhiim\n";
assert_eq!(y22d03(input, 1), 15);
assert_eq!(y22d03(input, 2), 10);