day 3: part 2: rust
This commit is contained in:
parent
6160b15a1f
commit
858e44bf8a
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
regex = "1.11.1"
|
|
@ -0,0 +1,26 @@
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut sum: u32 = 0;
|
||||||
|
let re1 = Regex::new(r"(do\(\)|don't\(\)|mul\([0-9]*,[0-9]*\))").unwrap();
|
||||||
|
let re2 = Regex::new(r"mul\(([0-9]*),([0-9]*)\)").unwrap();
|
||||||
|
let hay = std::io::read_to_string(std::io::stdin()).unwrap();
|
||||||
|
let mut i_do = true;
|
||||||
|
for (_, [does]) in re1.captures_iter(&hay).map(|c| c.extract()) {
|
||||||
|
if does == "do()" {
|
||||||
|
i_do = true;
|
||||||
|
}
|
||||||
|
if does == "don't()" {
|
||||||
|
i_do = false;
|
||||||
|
}
|
||||||
|
if i_do {
|
||||||
|
for (_, [num1, num2]) in re2.captures_iter(does).map(|c| c.extract()) {
|
||||||
|
let num1: u32 = num1.parse().unwrap();
|
||||||
|
let num2: u32 = num2.parse().unwrap();
|
||||||
|
sum += num1 * num2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("{}", sum);
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue