From 386b296c473851dea022b1e4b3cb3ae6e2e9cede Mon Sep 17 00:00:00 2001 From: endernon Date: Tue, 3 Dec 2024 18:30:30 +0000 Subject: [PATCH] day 3 part 1 --- 2024/day3/part1/Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++ 2024/day3/part1/Cargo.toml | 7 +++++ 2024/day3/part1/src/main.rs | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 2024/day3/part1/Cargo.lock create mode 100644 2024/day3/part1/Cargo.toml create mode 100644 2024/day3/part1/src/main.rs diff --git a/2024/day3/part1/Cargo.lock b/2024/day3/part1/Cargo.lock new file mode 100644 index 0000000..4c00902 --- /dev/null +++ b/2024/day3/part1/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/part1/Cargo.toml b/2024/day3/part1/Cargo.toml new file mode 100644 index 0000000..0275d9d --- /dev/null +++ b/2024/day3/part1/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/part1/src/main.rs b/2024/day3/part1/src/main.rs new file mode 100644 index 0000000..8b04ac0 --- /dev/null +++ b/2024/day3/part1/src/main.rs @@ -0,0 +1,48 @@ +use regex::Regex; +use std::fs::read_to_string; + +fn main() { + let lines: Vec = read_to_string("input.txt") + .unwrap() + .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) +} + + +fn mul_parse(thelines: Vec, pattern: Regex) -> Vec{ + let mut mulvec: Vec = Vec::new(); + for i in thelines { + let tempvec: Vec = + pattern.find_iter(&i) + .filter_map(|fr| fr.as_str().parse::().ok()).collect(); + + for i2 in tempvec { + mulvec.push(i2); + } + + } + mulvec + +} + +fn mul_process(mulvec: Vec, pattern: Regex) -> i64 { + let mut mulint: i64 = 0; + for i in mulvec { + let captures = pattern.captures(&i).unwrap(); + let capture1 = captures.get(1).unwrap().as_str().parse::().unwrap(); + + let capture2 = captures.get(2).unwrap().as_str().parse::().unwrap(); + mulint += (capture1 * capture2) as i64; + } + + + mulint + +} \ No newline at end of file