From f5e79284a18e77a4f20b10f15cb1e0fde7d0ce31 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 3 Dec 2024 09:37:51 -0600 Subject: [PATCH] Day 3 part 1 done --- 2024/rust/src/day3.rs | 60 +++++++++++++++++++++++++++++++++++++-- 2024/rust/src/template.rs | 1 + 2 files changed, 59 insertions(+), 2 deletions(-) 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); }