From 7a38d30449e72cd846c22515747b4070e2e4a2e3 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Tue, 3 Dec 2024 21:27:56 +0300 Subject: [PATCH] Day 3 --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 811ea06..93cd5eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "either" version = "1.13.0" @@ -17,9 +26,45 @@ dependencies = [ "either", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "testing" version = "0.1.0" dependencies = [ "itertools", + "regex", ] diff --git a/Cargo.toml b/Cargo.toml index 87cb887..4894e59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ all = "warn" [dependencies] itertools = "0.13.0" +regex = "1.11.1" diff --git a/src/main.rs b/src/main.rs index 22ad29c..b6cfd7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,9 +101,46 @@ mod day2 { } } +mod day3 { + use std::fs::read_to_string; + + use regex::Regex; + + pub fn task1() { + let rx = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap(); + let mut result: i64 = 0; + for c in rx.captures_iter(&read_to_string("input.txt").unwrap()) { + let (_, [a, b]) = c.extract(); + result += a.parse::().unwrap() * b.parse::().unwrap(); + } + println!("{result}"); + } + + pub fn task2() { + let rx = Regex::new(r"(mul)\((\d{1,3}),(\d{1,3})\)|do()()()\(\)|do(n't)()()\(\)").unwrap(); + let mut enabled = true; + let mut result: i64 = 0; + for c in rx.captures_iter(&read_to_string("input.txt").unwrap()) { + let (_, [op, a, b]) = c.extract(); + match op { + "" => enabled = true, + "n't" => enabled = false, + "mul" if enabled => { + result += a.parse::().unwrap() * b.parse::().unwrap(); + } + "mul" => {} + _ => unreachable!("{op}"), + } + } + println!("{result}"); + } +} + fn main() { // day1::task1(); // day1::task1(); - day2::task1(); - day2::task2(); + // day2::task1(); + // day2::task2(); + day3::task1(); + day3::task2(); }