diff --git a/2024/day3/part2/Cargo.lock b/2024/day3/part2/Cargo.lock new file mode 100644 index 0000000..4c00902 --- /dev/null +++ b/2024/day3/part2/Cargo.lock @@ -0,0 +1,54 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "day3" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[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" diff --git a/2024/day3/part2/Cargo.toml b/2024/day3/part2/Cargo.toml new file mode 100644 index 0000000..0275d9d --- /dev/null +++ b/2024/day3/part2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2021" + +[dependencies] +regex = "1.11.1" diff --git a/2024/day3/part2/src/main.rs b/2024/day3/part2/src/main.rs index 8b04ac0..622af99 100644 --- a/2024/day3/part2/src/main.rs +++ b/2024/day3/part2/src/main.rs @@ -7,12 +7,11 @@ fn main() { .lines() .map(String::from) .collect(); - let pattern1 = Regex::new(r"mul\((\d+,\d+)\)").unwrap(); - let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap(); - - let mut finalvec1 = mul_parse(lines, pattern1); - let finalint = mul_process(finalvec1,pattern2); - println!("{:?}",finalint) + let pattern1 = Regex::new(r"(mul\((\d+)\,(\d+)\))|(do\(\))|(don\'t\(\))").unwrap(); + let finalvec1 = mul_parse(lines, pattern1.clone()); + let finalint = mul_process(finalvec1,pattern1.clone()); + // println!("{:?}",finalint) + println!("{finalint}") } @@ -28,18 +27,40 @@ fn mul_parse(thelines: Vec, pattern: Regex) -> Vec{ } } + println!("mulvec is {:?}",mulvec); mulvec } fn mul_process(mulvec: Vec, pattern: Regex) -> i64 { + let mut enabled = true; let mut mulint: i64 = 0; + let matchmul = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap(); + let matchdo = Regex::new(r"do\(\)").unwrap(); + let matchdont = Regex::new(r"don\'t\(\)").unwrap(); for i in mulvec { - let captures = pattern.captures(&i).unwrap(); - let capture1 = captures.get(1).unwrap().as_str().parse::().unwrap(); + if let Some(t) = matchdo.find(&i) { + println!("matchdo"); + enabled = true; + } + else if let Some(t) = matchdont.find(&i) { + println!("matchdont"); + enabled = false; + } + else if let Some(t) = matchmul.find(&i) { + if enabled { + println!("matchmul"); + let captures = pattern.captures(&i).unwrap(); + let capture1 = captures.get(2).unwrap().as_str().parse::().unwrap(); + let capture2 = captures.get(3).unwrap().as_str().parse::().unwrap(); + mulint += (capture1 * capture2) as i64; + } + } + + + + - let capture2 = captures.get(2).unwrap().as_str().parse::().unwrap(); - mulint += (capture1 * capture2) as i64; }