day 3: part 1: rust

This commit is contained in:
Samuel Ortion 2024-12-03 12:13:40 +01:00
parent 160145a983
commit 6160b15a1f
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
2 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,7 @@
[package]
name = "part1"
version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1.11.1"

View File

@ -0,0 +1,14 @@
use regex::Regex;
fn main() -> std::io::Result<()> {
let mut sum: u32 = 0;
let re = Regex::new(r"mul\(([0-9]*),([0-9]*)\)").unwrap();
let hay = std::io::read_to_string(std::io::stdin()).unwrap();
for (_, [num1, num2]) in re.captures_iter(&hay).map(|c| c.extract()) {
let num1: u32 = num1.parse().unwrap();
let num2: u32 = num2.parse().unwrap();
sum += num1 * num2;
}
println!("{}", sum);
Ok(())
}