diff --git a/2024/rust/src/day3.rs b/2024/rust/src/day3.rs index 235d012..e2c137b 100644 --- a/2024/rust/src/day3.rs +++ b/2024/rust/src/day3.rs @@ -2,7 +2,58 @@ mod prelude; pub use crate::prelude::*; fn main() { - show_answers(0, 0); + let input = day_input(3); + show_answers(sum_muls(&input), 0); +} + +struct MulParser { + left: Option, + right: Option, +} + +fn sum_muls(input: &str) -> usize { + let mut sum = 0; + let mut s = &input[..]; + while let Some(i) = s.find("mul(") { + s = &s[i + 4..]; + let mut left: Option = None; + 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]; + if token.len() > 3 { + break; + } + } + ',' => { + println!("token at , {token:?}"); + if left.is_some() { + break; + } + left = Some(token.parse().unwrap()); + s = &s[token.len() + 1..]; + token = &s[0..0]; + } + ')' => { + println!("token at ) {token:?}"); + if right.is_some() { + break; + } + right = Some(token.parse().unwrap()); + s = &s[token.len() + 1..]; + token = &s[0..0]; + } + _ => break, + } + } + if let (Some(left), Some(right)) = (left, right) { + sum += left * right; + } + } + return sum; } #[cfg(test)] @@ -10,5 +61,10 @@ mod tests { use super::*; #[test] - fn test() {} + fn test() { + assert_eq!( + sum_muls("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"), + 161 + ) + } } diff --git a/2024/rust/src/template.rs b/2024/rust/src/template.rs index 235d012..1118127 100644 --- a/2024/rust/src/template.rs +++ b/2024/rust/src/template.rs @@ -2,6 +2,7 @@ mod prelude; pub use crate::prelude::*; fn main() { + let _input = day_input(0); show_answers(0, 0); }