use advent_of_code::include_aoc; use advent_of_code::strings::parsenumber; use regex::{Captures, Regex}; include_aoc!(2024, 03); fn parse_part1(input: Vec>) -> u32 { let mut sum = 0; let (mut i, mut j): (u32, u32); for capture in input { i = parsenumber(capture.name("i").unwrap().as_str()); j = parsenumber(capture.name("j").unwrap().as_str()); sum += i * j } sum } fn parse_part2(input: Vec>) -> u32 { let mut sum = 0; let mut parse = true; let (mut i, mut j): (u32, u32); for capture in input { match (capture.get(1).unwrap().as_str(), parse) { ("do()", _) => parse = true, ("don't()", _) => parse = false, (_, true) => { i = parsenumber(capture.name("i").unwrap().as_str()); j = parsenumber(capture.name("j").unwrap().as_str()); sum += i * j }, (_, _) => {}, } } sum } fn main() { let part1 = Regex::new(r"mul\((?\d{1,3}),(?\d{1,3})\)").unwrap(); let part2 = Regex::new(r"(mul\((?\d{1,3}),(?\d{1,3})\)|do\(\)|don't\(\))").unwrap(); let sum = parse_part1(part1.captures_iter(DATA).collect()); println!("The Cleaned output is: {}", sum); let sum = parse_part2(part2.captures_iter(DATA).collect()); println!("The Cleaned with check output is: {}", sum); }