Day 3 part 1 done

This commit is contained in:
Daniel Flanagan 2024-12-03 09:37:51 -06:00
parent 95d88256e0
commit f5e79284a1
2 changed files with 59 additions and 2 deletions

View file

@ -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<usize>,
right: Option<usize>,
}
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<usize> = None;
let mut right: Option<usize> = 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
)
}
}

View file

@ -2,6 +2,7 @@ mod prelude;
pub use crate::prelude::*;
fn main() {
let _input = day_input(0);
show_answers(0, 0);
}