adventofcode/src/bin/2024/03.rs

46 Zeilen
1,4 KiB
Rust

2024-12-03 09:18:33 +01:00
use advent_of_code::strings::parsenumber;
#[allow(unused_imports)]
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);
2024-12-16 08:34:14 +01:00
fn parse_part1(input : Vec<Captures<'_>>) -> u32 {
2024-12-03 09:18:33 +01:00
let mut sum = 0;
2024-12-16 08:34:14 +01:00
let (mut i,mut j): (u32, u32);
for capture in input {
i = parsenumber(capture.name("i").unwrap().as_str());
j=parsenumber(capture.name("j").unwrap().as_str());
sum += i * j
2024-12-03 09:18:33 +01:00
}
2024-12-16 08:34:14 +01:00
sum
}
fn parse_part2(input : Vec<Captures<'_>>)->u32 {
2024-12-03 09:18:33 +01:00
let mut sum = 0;
2024-12-16 08:34:14 +01:00
let mut parse = true;
let (mut i,mut j): (u32, u32);
for capture in input {
2024-12-03 09:18:33 +01:00
match (capture.get(1).unwrap().as_str(), parse) {
("do()", _) => parse = true,
("don't()", _) => parse = false,
(_, true) => {
2024-12-16 08:34:14 +01:00
i = parsenumber(capture.name("i").unwrap().as_str());
j = parsenumber(capture.name("j").unwrap().as_str());
sum += i * j
},
(_, _) => {},
2024-12-03 09:18:33 +01:00
}
}
2024-12-16 08:34:14 +01:00
sum
}
fn main() {
let part1 = Regex::new(r"mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)").unwrap();
let part2 = Regex::new(r"(mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)|do\(\)|don't\(\))").unwrap();
let sum = parse_part1(part1.captures_iter(DATA).collect());
println!("The Cleaned output is: {}", sum);
let sum = parse_part2(part2.captures_iter(DATA).collect());
2024-12-03 09:18:33 +01:00
println!("The Cleaned with check output is: {}", sum);
}