Solution for 2024 day 3

Dieser Commit ist enthalten in:
Sebastian Tobie 2024-12-03 09:18:33 +01:00
Ursprung 587ef24505
Commit b626b224b4

42
src/bin/2024/03.rs Normale Datei
Datei anzeigen

@ -0,0 +1,42 @@
use advent_of_code::strings::parsenumber;
#[allow(unused_imports)]
use adventofcode_macros::{include_data, include_example};
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) => {
let a = parsenumber(capture.name("i").unwrap().as_str());
let b = parsenumber(capture.name("j").unwrap().as_str());
sum += a * b
}
(_, _) => {}
}
}
println!("The Cleaned with check output is: {}", sum);
}