This commit is contained in:
dobiadi
2024-12-03 19:32:02 +01:00
parent 3a3ca31626
commit 056644562b
8 changed files with 172 additions and 0 deletions

45
day3/rust/src/main.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::io::Read;
use regex::Regex;
fn main() {
let mut stdin = std::io::stdin();
let mut input = String::new();
stdin.read_to_string(&mut input).unwrap();
let regex = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)").unwrap();
let mut enabled = true;
let mut sum = 0;
let mut sum2 = 0;
for capture in regex.captures_iter(&input) {
let matched = capture.get(0).unwrap().as_str();
match matched {
"do()" => {
enabled = true;
continue;
}
"don't()" => {
enabled = false;
continue;
}
_ => (),
}
let (a, b): (isize, isize) = (
capture.get(1).unwrap().as_str().parse().unwrap(),
capture.get(2).unwrap().as_str().parse().unwrap(),
);
sum += a * b;
if enabled {
sum2 += a * b;
}
}
println!("{}", sum);
println!("{}", sum2);
}