adventofcode/days/2024/03.rs

45 Zeilen
1,4 KiB
Rust

use advent_of_code::include_aoc;
2024-12-03 09:18:33 +01:00
use advent_of_code::strings::parsenumber;
use regex::{Captures, Regex};
include_aoc!(2024, 03);
2024-12-03 09:18:33 +01:00
2024-12-16 20:42:16 +01:00
fn parse_part1(input: Vec<Captures<'_>>) -> u32 {
2024-12-03 09:18:33 +01:00
let mut sum = 0;
2024-12-16 20:42:16 +01:00
let (mut i, mut j): (u32, u32);
2024-12-16 08:34:14 +01:00
for capture in input {
i = parsenumber(capture.name("i").unwrap().as_str());
2024-12-16 20:42:16 +01:00
j = parsenumber(capture.name("j").unwrap().as_str());
2024-12-16 08:34:14 +01:00
sum += i * j
2024-12-03 09:18:33 +01:00
}
2024-12-16 08:34:14 +01:00
sum
}
2024-12-16 20:42:16 +01:00
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;
2024-12-16 20:42:16 +01:00
let (mut i, mut j): (u32, u32);
2024-12-16 08:34:14 +01:00
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);
}