use regex::Regex; pub fn p1(input: &str) -> String { let re = Regex::new(r"mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap(); let mut result: u32 = 0; for (_, [x, y]) in re.captures_iter(input).map(|cs| cs.extract()) { result += x.parse::().unwrap() * y.parse::().unwrap(); } result.to_string() } pub fn p2(input: &str) -> String { let re = Regex::new(r"do\(\)|don't\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap(); let mut doing = true; let mut result: u32 = 0; for cs in re.captures_iter(input) { let mut it = cs.iter().flatten().map(|m| m.as_str()); match it.next().unwrap() { "do()" => doing = true, "don't()" => doing = false, mul if doing && mul.starts_with("mul") => { let mut next = || it.next().unwrap().parse::().unwrap(); result += next() * next(); } _ => (), } } result.to_string() }