y2024d3 p2 still incorrect

This commit is contained in:
Fabian Schmidt 2024-12-03 10:52:57 +01:00
parent 1db00c45db
commit 4e6399f5e2

View File

@ -10,8 +10,8 @@ pub fn process_part1(input: &str) -> i32 {
// Could use this regex but wouldn't know how to solve part 2 // Could use this regex but wouldn't know how to solve part 2
// /mul\([0-9]{1,3},[0-9]{1,3}\)/g // /mul\([0-9]{1,3},[0-9]{1,3}\)/g
// /don't\(\).*do\(\)/g would select anything between don't() and do() // /don't\(\).*do\(\)/g would select anything between first don't() and last do()
// not sure how to invert this and combine // not sure how to fix, invert and combine this
fn extract_mul_pairs(line: &str) -> Vec<(i32, i32)> { fn extract_mul_pairs(line: &str) -> Vec<(i32, i32)> {
let reg = Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap(); let reg = Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap();
reg.find_iter(line) reg.find_iter(line)
@ -25,10 +25,32 @@ fn extract_mul_pairs(line: &str) -> Vec<(i32, i32)> {
.collect::<Vec<(i32, i32)>>() .collect::<Vec<(i32, i32)>>()
} }
fn remove_donts(line: &str) -> String {
let mut new_line = line.to_string();
loop {
println!("len {}", line.len());
let mut end_range = new_line.len() - 1;
if let Some(dont_idx) = new_line.find("don't()") {
if let Some(do_idx) = new_line.find("do()") {
if dont_idx < do_idx {
end_range = do_idx + 4;
}
}
println!("dont {dont_idx} to {end_range}");
new_line.replace_range(dont_idx..end_range, "");
} else {
return new_line;
}
}
}
pub fn process_part2(input: &str) -> i32 { pub fn process_part2(input: &str) -> i32 {
input input
.lines() .lines()
.map(extract_mul_pairs) .map(|line| {
let line = remove_donts(line);
extract_mul_pairs(&line)
})
.map(|pairs| pairs.iter().map(|(a, b)| a * b).sum::<i32>()) .map(|pairs| pairs.iter().map(|(a, b)| a * b).sum::<i32>())
.sum() .sum()
} }