From 1db00c45dbfbfa04f246c7b846eb6f34bd3b5519 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Tue, 3 Dec 2024 10:21:13 +0100 Subject: [PATCH] y2024d3 use regex for part 1 --- Cargo.lock | 6 +-- Cargo.toml | 3 -- y2024/Cargo.toml | 1 + y2024/src/days/d3.rs | 108 ++++++------------------------------------- 4 files changed, 17 insertions(+), 101 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55fee8c..193872a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,9 +14,6 @@ dependencies = [ [[package]] name = "genaoc" version = "0.1.0" -dependencies = [ - "regex", -] [[package]] name = "md5" @@ -107,3 +104,6 @@ version = "0.1.0" [[package]] name = "y2024" version = "0.1.0" +dependencies = [ + "regex", +] diff --git a/Cargo.toml b/Cargo.toml index 35d722f..ab40e13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,3 @@ members = [ [workspace.dependencies] utils = { git = "https://git.plobos.xyz/projects/PuzzleUtils.git" } - -[dependencies] -regex = "1.11.1" diff --git a/y2024/Cargo.toml b/y2024/Cargo.toml index 28f363e..e019528 100644 --- a/y2024/Cargo.toml +++ b/y2024/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +regex = "1.11.1" diff --git a/y2024/src/days/d3.rs b/y2024/src/days/d3.rs index 59cc9a0..dd9f3d9 100644 --- a/y2024/src/days/d3.rs +++ b/y2024/src/days/d3.rs @@ -1,3 +1,5 @@ +use regex::Regex; + pub fn process_part1(input: &str) -> i32 { input .lines() @@ -11,106 +13,22 @@ pub fn process_part1(input: &str) -> i32 { // /don't\(\).*do\(\)/g would select anything between don't() and do() // not sure how to invert this and combine fn extract_mul_pairs(line: &str) -> Vec<(i32, i32)> { - let mut pairs = Vec::new(); - for (idx, _) in line.match_indices("mul(") { - let mut invalid = false; - let mut comma_pos = 0; - let mut pair = ("".to_string(), "".to_string()); - // max length of parenthesis == 9 - for paren_idx in 0..9 { - let paren_content = match line.as_bytes().get(idx + 4 + paren_idx) { - Some(content) => *content as char, - None => { - invalid = true; - break; - } - }; - if paren_content == ')' { - if comma_pos == 0 { - invalid = true; - } - break; - } - if paren_content == ',' { - comma_pos = paren_idx; - continue; - } - if !paren_content.is_ascii_digit() { - invalid = true; - break; - } - if comma_pos == 0 { - pair.0.push(paren_content); - } else { - pair.1.push(paren_content); - } - } - if !invalid { - let a = pair.0.parse::().unwrap(); - let b = pair.1.parse::().unwrap(); - pairs.push((a, b)); - } - } - pairs -} - -fn extract_mul_pairs_dont(line: &str) -> Vec<(i32, i32)> { - let mut pairs = Vec::new(); - for (idx, _) in line.match_indices("mul(") { - if let Some(dont_func) = line[..idx].rfind("don't()") { - if let Some(do_func) = line[..idx].rfind("do()") { - if do_func < dont_func { - continue; - } - } else { - continue; - } - } - let mut invalid = false; - let mut comma_pos = 0; - let mut pair = ("".to_string(), "".to_string()); - // max length of parenthesis == 9 - for paren_idx in 0..9 { - let paren_content = match line.as_bytes().get(idx + 4 + paren_idx) { - Some(content) => *content as char, - None => { - invalid = true; - break; - } - }; - if paren_content == ')' { - if comma_pos == 0 { - invalid = true; - } - break; - } - if paren_content == ',' { - comma_pos = paren_idx; - continue; - } - if !paren_content.is_ascii_digit() { - invalid = true; - break; - } - if comma_pos == 0 { - pair.0.push(paren_content); - } else { - pair.1.push(paren_content); - } - } - if !invalid { - let a = pair.0.parse::().unwrap(); - let b = pair.1.parse::().unwrap(); - pairs.push((a, b)); - } - } - pairs + let reg = Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap(); + reg.find_iter(line) + .map(|m| { + let match_str = m.as_str(); + let (a, b) = match_str[4..match_str.len() - 1].split_once(",").unwrap(); + let a = a.parse::().unwrap(); + let b = b.parse::().unwrap(); + (a, b) + }) + .collect::>() } pub fn process_part2(input: &str) -> i32 { input .lines() - .map(extract_mul_pairs_dont) + .map(extract_mul_pairs) .map(|pairs| pairs.iter().map(|(a, b)| a * b).sum::()) .sum() }