adventofcode/days/2024/03.rs

44 Zeilen
1,4 KiB
Rust

use advent_of_code::include_aoc;
use advent_of_code::strings::parsenumber;
use regex::{Captures, Regex};
include_aoc!(2024, 03);
fn parse_part1(input: Vec<Captures<'_>>) -> u32 {
let mut sum = 0;
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
}
sum
}
fn parse_part2(input: Vec<Captures<'_>>) -> u32 {
let mut sum = 0;
let mut parse = true;
let (mut i, mut j): (u32, u32);
for capture in input {
match (capture.get(1).unwrap().as_str(), parse) {
("do()", _) => parse = true,
("don't()", _) => parse = false,
(_, true) => {
i = parsenumber(capture.name("i").unwrap().as_str());
j = parsenumber(capture.name("j").unwrap().as_str());
sum += i * j
},
(_, _) => {},
}
}
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());
println!("The Cleaned with check output is: {}", sum);
}