diff --git a/2024/rust/src/day3.rs b/2024/rust/src/day3.rs index e2c137b..ce24104 100644 --- a/2024/rust/src/day3.rs +++ b/2024/rust/src/day3.rs @@ -3,7 +3,7 @@ pub use crate::prelude::*; fn main() { let input = day_input(3); - show_answers(sum_muls(&input), 0); + show_answers(sum_muls(&input), sum_muls_with_do_and_dont(&input)); } struct MulParser { @@ -20,7 +20,6 @@ fn sum_muls(input: &str) -> usize { let mut right: Option = None; let mut token = &s[0..0]; for c in s.chars() { - println!("c: {c}"); match c { '0'..='9' => { token = &s[0..token.len() + 1]; @@ -29,7 +28,6 @@ fn sum_muls(input: &str) -> usize { } } ',' => { - println!("token at , {token:?}"); if left.is_some() { break; } @@ -38,7 +36,6 @@ fn sum_muls(input: &str) -> usize { token = &s[0..0]; } ')' => { - println!("token at ) {token:?}"); if right.is_some() { break; } @@ -53,7 +50,26 @@ fn sum_muls(input: &str) -> usize { sum += left * right; } } - return sum; + sum +} + +fn sum_muls_with_do_and_dont(input: &str) -> usize { + let mut sum = 0; + let mut s = &input[..]; + let mut mul_rest = true; + while let Some(i) = s.find("don't()") { + sum += sum_muls(&s[0..i]); + s = &s[i..]; + mul_rest = false; + if let Some(i) = s.find("do()") { + s = &s[i..]; + mul_rest = true; + } + } + if mul_rest { + sum += sum_muls(s); + } + sum } #[cfg(test)]