2024-12-03 09:18:33 +01:00
|
|
|
use advent_of_code::strings::parsenumber;
|
|
|
|
#[allow(unused_imports)]
|
2024-12-06 16:56:47 +01:00
|
|
|
use advent_of_code::{include_data, include_example};
|
2024-12-03 09:18:33 +01:00
|
|
|
use regex::{Captures, Regex};
|
|
|
|
|
|
|
|
include_data!(DATA 2024 03);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let part1re = Regex::new(r"mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)").unwrap();
|
|
|
|
let multiplies: Vec<(u32, u32)> = part1re
|
|
|
|
.captures_iter(DATA)
|
|
|
|
.map(|capture| {
|
|
|
|
let i = capture.name("i").unwrap().as_str();
|
|
|
|
let j = capture.name("j").unwrap().as_str();
|
|
|
|
let a = parsenumber(i);
|
|
|
|
let b = parsenumber(j);
|
|
|
|
(a, b)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let mut sum = 0;
|
|
|
|
for (i, j) in multiplies {
|
|
|
|
sum += i * j;
|
|
|
|
}
|
|
|
|
println!("The Cleaned output is: {}", sum);
|
|
|
|
let part2re = Regex::new(r"(mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)|do\(\)|don't\(\))").unwrap();
|
|
|
|
let inputs: Vec<Captures<'_>> = part2re.captures_iter(DATA).collect();
|
|
|
|
let mut parse = true;
|
|
|
|
let mut sum = 0;
|
|
|
|
for capture in inputs {
|
|
|
|
match (capture.get(1).unwrap().as_str(), parse) {
|
|
|
|
("do()", _) => parse = true,
|
|
|
|
("don't()", _) => parse = false,
|
|
|
|
(_, true) => {
|
2024-12-08 02:04:16 +01:00
|
|
|
let a = parsenumber::<u32>(capture.name("i").unwrap().as_str());
|
|
|
|
let b = parsenumber::<u32>(capture.name("j").unwrap().as_str());
|
2024-12-03 09:18:33 +01:00
|
|
|
sum += a * b
|
2024-12-14 11:47:02 +01:00
|
|
|
},
|
|
|
|
(_, _) => {},
|
2024-12-03 09:18:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("The Cleaned with check output is: {}", sum);
|
|
|
|
}
|