y2024d3p2 simplified, anyway, I lost too much time on it already

This commit is contained in:
Fabian Schmidt 2024-12-03 13:03:49 +01:00
parent 5b2e4a38d1
commit daead25413

View File

@ -26,56 +26,26 @@ fn extract_mul_pairs(line: &str) -> Vec<(i32, i32)> {
} }
fn extract_mul_pairs_dont(line: &str) -> Vec<(i32, i32)> { fn extract_mul_pairs_dont(line: &str) -> Vec<(i32, i32)> {
let mut pairs = Vec::new(); let reg = Regex::new(r"(mul\([0-9]{1,3},[0-9]{1,3}\))|(don't\(\)|do\(\))").unwrap();
for (idx, _) in line.match_indices("mul(") { let mut enabled = true;
if let Some(dont_func) = line[..idx].rfind("don't()") { reg.find_iter(line)
if let Some(do_func) = line[..idx].rfind("do()") { .map(|m| {
if do_func < dont_func { let match_str = m.as_str();
continue; if match_str == "don't()" {
} enabled = false;
} else if match_str == "do()" {
enabled = true;
}
if !["don't()", "do()"].contains(&match_str) && enabled {
let (a, b) = match_str[4..match_str.len() - 1].split_once(",").unwrap();
let a = a.parse::<i32>().unwrap();
let b = b.parse::<i32>().unwrap();
(a, b)
} else { } else {
continue; (0, 0)
} }
} })
let mut invalid = false; .collect::<Vec<(i32, i32)>>()
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::<i32>().unwrap();
let b = pair.1.parse::<i32>().unwrap();
pairs.push((a, b));
}
}
pairs
} }
pub fn process_part2(input: &str) -> i32 { pub fn process_part2(input: &str) -> i32 {