Solution for 2024 day 3
Dieser Commit ist enthalten in:
Ursprung
587ef24505
Commit
b626b224b4
1 geänderte Dateien mit 42 neuen und 0 gelöschten Zeilen
42
src/bin/2024/03.rs
Normale Datei
42
src/bin/2024/03.rs
Normale Datei
|
@ -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);
|
||||
}
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren