From 4e23bf30840b93dd56b3180f6514e608c821655a Mon Sep 17 00:00:00 2001 From: calcu1on Date: Sat, 29 Mar 2025 21:15:57 -0400 Subject: [PATCH] Solving day 3 - issue with regex detection of dos and donts. --- 2024/rust/src/day3.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/2024/rust/src/day3.rs b/2024/rust/src/day3.rs index f1a2e07..7ddff0f 100644 --- a/2024/rust/src/day3.rs +++ b/2024/rust/src/day3.rs @@ -19,14 +19,14 @@ pub fn run_day_3() -> Solution { let mut part2_sum = 0; let dont_regex = Regex::new("don't\\(\\)").unwrap(); let do_regex = Regex::new("do\\(\\)").unwrap(); - let mut donts: Vec = dont_regex.find(&file).map(|m| m.start() as u64).into_iter().collect(); - let mut dos: Vec = do_regex.find(&file).map(|m| m.start() as u64).into_iter().collect(); + let donts: Vec = dont_regex.find_iter(&file).map(|m| m.start() as u64).collect(); + let dos: Vec = do_regex.find_iter(&file).map(|m| m.start() as u64).collect(); + for mul in re.captures_iter(&file) { - let start: usize = mul.get(1).unwrap().start(); + let start: usize = mul.get(0).unwrap().start(); // get the closest number, that is lower than the start value let mut closest_dont = 0; for i in (0..start).rev() { - // remove 1 becasue we want to search in reverse. let mut index = i as u64; if donts.contains(&index) { closest_dont = index; @@ -42,8 +42,6 @@ pub fn run_day_3() -> Solution { } } - println!("{:?} -> {:?}", closest_do, closest_dont); - if closest_do > closest_dont { let num1: &u32 = &mul[1].parse().expect("Conversion failed"); let num2: &u32 = &mul[2].parse().expect("Conversion failed"); @@ -59,8 +57,6 @@ pub fn run_day_3() -> Solution { } } - // println!("{:?} -> {:?}", dos, donts); - // Final solutions here. let solution = Solution { day: 3,