diff --git a/src/bin/2024/03.rs b/src/bin/2024/03.rs new file mode 100644 index 0000000..04f08f9 --- /dev/null +++ b/src/bin/2024/03.rs @@ -0,0 +1,42 @@ +use advent_of_code::strings::parsenumber; +#[allow(unused_imports)] +use adventofcode_macros::{include_data, include_example}; +use regex::{Captures, Regex}; + +include_data!(DATA 2024 03); + +fn main() { + let part1re = Regex::new(r"mul\((?\d{1,3}),(?\d{1,3})\)").unwrap(); + let multiplies: Vec<(u32, u32)> = part1re + .captures_iter(DATA) + .map(|capture| { + let i = capture.name("i").unwrap().as_str(); + let j = capture.name("j").unwrap().as_str(); + let a = parsenumber(i); + let b = parsenumber(j); + (a, b) + }) + .collect(); + let mut sum = 0; + for (i, j) in multiplies { + sum += i * j; + } + println!("The Cleaned output is: {}", sum); + let part2re = Regex::new(r"(mul\((?\d{1,3}),(?\d{1,3})\)|do\(\)|don't\(\))").unwrap(); + let inputs: Vec> = part2re.captures_iter(DATA).collect(); + let mut parse = true; + let mut sum = 0; + for capture in inputs { + match (capture.get(1).unwrap().as_str(), parse) { + ("do()", _) => parse = true, + ("don't()", _) => parse = false, + (_, true) => { + let a = parsenumber(capture.name("i").unwrap().as_str()); + let b = parsenumber(capture.name("j").unwrap().as_str()); + sum += a * b + } + (_, _) => {} + } + } + println!("The Cleaned with check output is: {}", sum); +}