From b250c122301c599379409fd0405d83b42e14813d Mon Sep 17 00:00:00 2001 From: Rh4096 Date: Tue, 3 Dec 2024 18:05:54 +0700 Subject: [PATCH] day 3 --- Cargo.lock | 45 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 8 ++++++- src/solutions/day_10.rs | 0 src/solutions/day_11.rs | 0 src/solutions/day_12.rs | 0 src/solutions/day_13.rs | 0 src/solutions/day_14.rs | 0 src/solutions/day_15.rs | 0 src/solutions/day_16.rs | 0 src/solutions/day_17.rs | 0 src/solutions/day_18.rs | 0 src/solutions/day_19.rs | 0 src/solutions/day_20.rs | 0 src/solutions/day_21.rs | 0 src/solutions/day_22.rs | 0 src/solutions/day_23.rs | 0 src/solutions/day_24.rs | 0 src/solutions/day_25.rs | 0 src/solutions/day_3.rs | 51 +++++++++++++++++++++++++++++++++++++++++ src/solutions/day_4.rs | 0 src/solutions/day_5.rs | 0 src/solutions/day_6.rs | 0 src/solutions/day_7.rs | 0 src/solutions/day_8.rs | 0 src/solutions/day_9.rs | 0 src/solutions/mod.rs | 12 ++++++++-- 27 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 src/solutions/day_10.rs create mode 100644 src/solutions/day_11.rs create mode 100644 src/solutions/day_12.rs create mode 100644 src/solutions/day_13.rs create mode 100644 src/solutions/day_14.rs create mode 100644 src/solutions/day_15.rs create mode 100644 src/solutions/day_16.rs create mode 100644 src/solutions/day_17.rs create mode 100644 src/solutions/day_18.rs create mode 100644 src/solutions/day_19.rs create mode 100644 src/solutions/day_20.rs create mode 100644 src/solutions/day_21.rs create mode 100644 src/solutions/day_22.rs create mode 100644 src/solutions/day_23.rs create mode 100644 src/solutions/day_24.rs create mode 100644 src/solutions/day_25.rs create mode 100644 src/solutions/day_3.rs create mode 100644 src/solutions/day_4.rs create mode 100644 src/solutions/day_5.rs create mode 100644 src/solutions/day_6.rs create mode 100644 src/solutions/day_7.rs create mode 100644 src/solutions/day_8.rs create mode 100644 src/solutions/day_9.rs diff --git a/Cargo.lock b/Cargo.lock index 876ed03..bea35fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,13 +2,29 @@ # 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 = "aoc2024" version = "0.1.0" dependencies = [ + "regex", "thiserror", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "proc-macro2" version = "1.0.92" @@ -27,6 +43,35 @@ dependencies = [ "proc-macro2", ] +[[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 = "syn" version = "2.0.90" diff --git a/Cargo.toml b/Cargo.toml index f736cff..11a74ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" [dependencies] + regex = "1" thiserror = "2" diff --git a/src/main.rs b/src/main.rs index f328e4d..ba7a4b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1 +1,7 @@ -fn main() {} +use aoc2024::solutions::{day_3::AocDayThreeSolution, AocSolution}; + +fn main() { + let sol = AocDayThreeSolution; + + println!("p1: {}\np2: {}", sol.part_one(), sol.part_two()) +} diff --git a/src/solutions/day_10.rs b/src/solutions/day_10.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_11.rs b/src/solutions/day_11.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_12.rs b/src/solutions/day_12.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_13.rs b/src/solutions/day_13.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_14.rs b/src/solutions/day_14.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_15.rs b/src/solutions/day_15.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_16.rs b/src/solutions/day_16.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_17.rs b/src/solutions/day_17.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_18.rs b/src/solutions/day_18.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_19.rs b/src/solutions/day_19.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_20.rs b/src/solutions/day_20.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_21.rs b/src/solutions/day_21.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_22.rs b/src/solutions/day_22.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_23.rs b/src/solutions/day_23.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_24.rs b/src/solutions/day_24.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_25.rs b/src/solutions/day_25.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_3.rs b/src/solutions/day_3.rs new file mode 100644 index 0000000..6936425 --- /dev/null +++ b/src/solutions/day_3.rs @@ -0,0 +1,51 @@ +use std::sync::LazyLock; + +use regex::Regex; + +use super::AocSolution; + +static MUL: LazyLock = + LazyLock::new(|| Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap()); + +static COND_MUL: LazyLock = + LazyLock::new(|| Regex::new(r"(mul\([0-9]{1,3},[0-9]{1,3}\))|(do\(\))|(don't\(\))").unwrap()); + +#[derive(Clone, Copy)] +pub struct AocDayThreeSolution; + +fn as_mul(s: &str) -> u64 { + let (a, b) = s[4..(s.len() - 1)] + .split_once(',') + .map(|(a, b)| (a.parse::().unwrap(), b.parse::().unwrap())) + .unwrap(); + + a * b +} + +impl AocSolution for AocDayThreeSolution { + type Output = u64; + const INPUT: &str = include_str!("../../input/day3.txt"); + + fn part_one(&self) -> Self::Output { + MUL.find_iter(Self::INPUT) + .map(|x| x.as_str()) + .map(as_mul) + .sum() + } + + fn part_two(&self) -> Self::Output { + COND_MUL + .find_iter(Self::INPUT) + .map(|x| x.as_str()) + .fold((0, true), |(sum, flag), cut| { + if cut.starts_with("mul") && flag { + (sum + as_mul(cut), flag) + } else if cut == "do()" { + (sum, true) + } else { + (sum, false) + } + }) + .0 + } +} diff --git a/src/solutions/day_4.rs b/src/solutions/day_4.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_5.rs b/src/solutions/day_5.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_6.rs b/src/solutions/day_6.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_7.rs b/src/solutions/day_7.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_8.rs b/src/solutions/day_8.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/day_9.rs b/src/solutions/day_9.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 7a9aef4..ce13b4e 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -1,5 +1,13 @@ -pub mod day_1; -pub mod day_2; +macro_rules! define_solution { + ($($id:ident),+ $(,)?) => { + $(pub mod $id;)+ + }; +} + +define_solution!( + day_1, day_2, day_3, day_4, day_5, day_6, day_7, day_8, day_9, day_10, day_11, day_12, day_13, + day_14, day_15, day_16, day_17, day_18, day_19, day_20, day_21, day_22, day_23, day_24, day_25, +); pub trait AocSolution { type Output;