day_3: part 2 done

This commit is contained in:
2024-12-05 01:04:43 +01:00
parent 3c5facffae
commit 7d05694da6
3 changed files with 58 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ https://adventofcode.com/2024
| :---: | :--- | :---: | :---: | :--- | | :---: | :--- | :---: | :---: | :--- |
| 01 | Historian Hysteria | :pushpin: | :pushpin: | Arrays | | 01 | Historian Hysteria | :pushpin: | :pushpin: | Arrays |
| 02 | Red-Nosed Reports | :pushpin: | :pushpin: | Bidirectional arrays | | 02 | Red-Nosed Reports | :pushpin: | :pushpin: | Bidirectional arrays |
| 03 | Mull It Over | | | Regex | | 03 | Mull It Over | :pushpin: | :pushpin: | Regex |
| 04 | | | | | 04 | | | |
| 05 | | | | | 05 | | | |
| 06 | | | | | 06 | | | |

View File

@@ -27,4 +27,10 @@ pub fn historian_hysteria() {
"The result of the sum of multiplications is {}", "The result of the sum of multiplications is {}",
multiplication_added_result multiplication_added_result
); );
let conditional_multiplication_added_result =
tue_03::mull_it_over_conditional("./assets/day_3_instruction_input");
println!(
"The result of the conditional sum of multiplications is {}",
conditional_multiplication_added_result
);
} }

View File

@@ -18,6 +18,57 @@ pub fn mull_it_over(input: &str) -> MulNumber {
sum_result sum_result
} }
pub fn mull_it_over_conditional(input: &str) -> MulNumber {
let instruction_input = read_instruction_input(input);
let find_condition_and_multiply_regex =
Regex::new(r"(do\(\)|don't\(\).*?mul\([0-9]+,[0-9]+\)|.*?mul\([0-9]+,[0-9]+\))").unwrap();
let separate_condition_and_multiply_regex =
Regex::new(r"(do\(\)|don't\(\)).*?(mul\([0-9]+,[0-9]+\))").unwrap();
let mul_regex = Regex::new(r"(mul\([0-9]+,[0-9]+\))").unwrap();
let conditional_regex = Regex::new(r"(do\(\)|don't\(\))").unwrap();
let mut sum_result: MulNumber = 0;
let mut current_do_condition: bool = true;
for conditional_mul in find_condition_and_multiply_regex.captures_iter(&instruction_input) {
if let Some(operation_pair) =
separate_condition_and_multiply_regex.captures(&conditional_mul[1])
{
// conditional AND mul present
let next_conditional = conditional_regex.captures(&operation_pair[1]);
if let Some(c) = next_conditional {
if &c[1] == "do()" {
current_do_condition = true;
} else {
current_do_condition = false;
}
}
if let Some(o) = mul_regex.captures(&operation_pair[2]) {
if current_do_condition {
let multiplication_result = process_mul_operation(&o[1]);
sum_result += multiplication_result;
}
}
} else if let Some(c) = conditional_regex.captures(&conditional_mul[1]) {
// only conditional present
if &c[1] == "do()" {
current_do_condition = true;
} else {
current_do_condition = false;
}
} else if let Some(o) = mul_regex.captures(&conditional_mul[1]) {
// only mul present
if current_do_condition {
let multiplication_result = process_mul_operation(&o[1]);
sum_result += multiplication_result;
}
}
}
sum_result
}
fn process_mul_operation(operation: &str) -> MulNumber { fn process_mul_operation(operation: &str) -> MulNumber {
let mul_regex = Regex::new(r"mul\(([0-9]+),([0-9]+)\)").unwrap(); let mul_regex = Regex::new(r"mul\(([0-9]+),([0-9]+)\)").unwrap();