From 100dd2fd9ac6541e8d6a3b2987ed8163e78a4b22 Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Tue, 3 Dec 2024 13:07:21 +0100 Subject: [PATCH] Add day3 --- day3/Cargo.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ day3/Cargo.toml | 8 +++++++ day3/src/main.rs | 27 +++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 day3/Cargo.lock create mode 100644 day3/Cargo.toml create mode 100644 day3/src/main.rs diff --git a/day3/Cargo.lock b/day3/Cargo.lock new file mode 100644 index 0000000..a744edd --- /dev/null +++ b/day3/Cargo.lock @@ -0,0 +1,61 @@ +# 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 = [ + "once_cell", + "regex", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[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/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..7629559 --- /dev/null +++ b/day3/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2021" + +[dependencies] +once_cell = "1.20.2" +regex = "1.11.1" diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..19d8bfe --- /dev/null +++ b/day3/src/main.rs @@ -0,0 +1,27 @@ +use {once_cell::sync::Lazy, regex::Regex}; + +fn part1(input: &str) -> u32 { + static RE: Lazy = Lazy::new(|| Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap()); + RE.captures_iter(input) + .map(|c| { + c.extract::<2>() + .1 + .map(|d| d.parse::().unwrap()) + .iter() + .product::() + }) + .sum() +} + +fn part2(input: &str) -> u32 { + input + .split(r"do()") + .map(|s| part1(s.split(r"don't()").next().unwrap())) + .sum() +} + +fn main() { + let input = include_str!("../input.txt"); + println!("{}", part1(input)); + println!("{}", part2(input)); +}