diff --git a/2024/days/03/part2/Cargo.toml b/2024/days/03/part2/Cargo.toml new file mode 100644 index 0000000..7de3509 --- /dev/null +++ b/2024/days/03/part2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +[dependencies] +regex = "1.11.1" diff --git a/2024/days/03/part2/src/main.rs b/2024/days/03/part2/src/main.rs new file mode 100644 index 0000000..bedf443 --- /dev/null +++ b/2024/days/03/part2/src/main.rs @@ -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(()) +}