diff --git a/aoc2024/src/day03.rs b/aoc2024/src/day03.rs index 9350be5..19d30c8 100644 --- a/aoc2024/src/day03.rs +++ b/aoc2024/src/day03.rs @@ -1,13 +1,42 @@ +use regex::Regex; use shared::{Solution, Answer}; pub struct Day03; impl Solution for Day03 { fn part_1(&self, input: &str) -> Answer { - Answer::Unimplemented + let reg = Regex::new( + r"mul\((\d+),(\d+)\)" + ).unwrap(); + + let res : u32 = reg.captures_iter(input) + .map(|mat| mat.extract::<2>().1 + .map(|x| x.parse::().unwrap()) + .iter() + .product::() + ).sum(); + + Answer::Number(res as u64) } fn part_2(&self, input: &str) -> Answer { - Answer::Unimplemented + let reg = Regex::new( + r"(do)\(\)|(don't)\(\)|(mul)\((\d+),(\d+)\)" + ).unwrap(); + + let mut enabled = true; + let mut res = 0; + + for m in reg.captures_iter(input) { + if m.get(1).is_some() { + enabled = true; + } else if m.get(2).is_some() { + enabled = false; + } else if enabled { + res += m.get(4).unwrap().as_str().parse::().unwrap() * m.get(5).unwrap().as_str().parse::().unwrap(); + } + } + + Answer::Number(res as u64) } } diff --git a/src/main.rs b/src/main.rs index bffc73f..1c5c244 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ fn run_daily() { let data = load_actual(year, day).unwrap(); println!("Solved 1: {}", solution(&data, year, day, 1).unwrap()); - println!("Solved 2: {}", solution(&data, year, day, 1).unwrap()); + println!("Solved 2: {}", solution(&data, year, day, 2).unwrap()); } fn run_all() {