Function aoc::y23d01::y23d01

source ·
pub fn y23d01(input: &str, part: u32) -> u32
Expand description

The solution the day one challenge.

We take the input as a string and the part that we’re solving (in part 2 we treat number-words as their number equivalents). We loop through each line and if we’re in part two then we replace the number-words with their number equivalents (we wrap the number with its number-word so that if there are two words together we can account for both of them e.g., eightwo should provide both an 8 and a 2). Then we check each character from the input until we find a number match for the first digit and then we reverse the input and check again to find the second digit. We put them together, parse the result as an integer and add it to our total.

§Example

// probably read this from the input file...
let input = "one1twothree4\n24\nthree4five";
assert_eq!(y23d01(input, 1), 82);
assert_eq!(y23d01(input, 2), 73);