This commit is contained in:
Rh4096 2024-12-03 18:05:54 +07:00
parent 036937b3cd
commit b250c12230
27 changed files with 114 additions and 3 deletions

45
Cargo.lock generated
View file

@ -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"

View file

@ -4,4 +4,5 @@
version = "0.1.0"
[dependencies]
regex = "1"
thiserror = "2"

View file

@ -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())
}

0
src/solutions/day_10.rs Normal file
View file

0
src/solutions/day_11.rs Normal file
View file

0
src/solutions/day_12.rs Normal file
View file

0
src/solutions/day_13.rs Normal file
View file

0
src/solutions/day_14.rs Normal file
View file

0
src/solutions/day_15.rs Normal file
View file

0
src/solutions/day_16.rs Normal file
View file

0
src/solutions/day_17.rs Normal file
View file

0
src/solutions/day_18.rs Normal file
View file

0
src/solutions/day_19.rs Normal file
View file

0
src/solutions/day_20.rs Normal file
View file

0
src/solutions/day_21.rs Normal file
View file

0
src/solutions/day_22.rs Normal file
View file

0
src/solutions/day_23.rs Normal file
View file

0
src/solutions/day_24.rs Normal file
View file

0
src/solutions/day_25.rs Normal file
View file

51
src/solutions/day_3.rs Normal file
View file

@ -0,0 +1,51 @@
use std::sync::LazyLock;
use regex::Regex;
use super::AocSolution;
static MUL: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap());
static COND_MUL: LazyLock<Regex> =
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::<u64>().unwrap(), b.parse::<u64>().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
}
}

0
src/solutions/day_4.rs Normal file
View file

0
src/solutions/day_5.rs Normal file
View file

0
src/solutions/day_6.rs Normal file
View file

0
src/solutions/day_7.rs Normal file
View file

0
src/solutions/day_8.rs Normal file
View file

0
src/solutions/day_9.rs Normal file
View file

View file

@ -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;