From 8f6f15c47309bd5af5debb10a153d6d17460cbbe Mon Sep 17 00:00:00 2001 From: traxys Date: Tue, 3 Dec 2024 13:46:29 +0100 Subject: [PATCH] Day 3 --- Cargo.lock | 33 ++++++++++++++++++++++ Cargo.toml | 1 + src/bin/day3.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 src/bin/day3.rs diff --git a/Cargo.lock b/Cargo.lock index c6b55b2..9706f70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.18" @@ -75,6 +84,7 @@ dependencies = [ "color-eyre", "humantime", "itertools", + "regex", ] [[package]] @@ -318,11 +328,34 @@ 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 = "rustc-demangle" diff --git a/Cargo.toml b/Cargo.toml index 2765d75..8978658 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ clap = { version = "4.5.21", features = ["derive"] } color-eyre = "0.6.3" humantime = "2.1.0" itertools = "0.13.0" +regex = "1.11.1" diff --git a/src/bin/day3.rs b/src/bin/day3.rs new file mode 100644 index 0000000..ce13adb --- /dev/null +++ b/src/bin/day3.rs @@ -0,0 +1,74 @@ +use std::time::Instant; + +use aoc_2024::{load, print_res}; +use bstr::BString; +use regex::Regex; + +type Parsed<'a> = &'a str; + +pub fn parsing(input: &BString) -> color_eyre::Result { + core::str::from_utf8(input).map_err(Into::into) +} + +pub fn part1(input: Parsed) { + let regex = Regex::new(r#"mul\((\d+),(\d+)\)"#).unwrap(); + let mut total = 0; + + for capture in regex.captures_iter(input) { + let first: u64 = capture.get(1).unwrap().as_str().parse().unwrap(); + let second: u64 = capture.get(2).unwrap().as_str().parse().unwrap(); + + total += first * second; + } + + print_res!("Sum of multiplications: {total}"); +} + +pub fn part2(input: Parsed) { + let regex = Regex::new(r#"don't\(\)|do\(\)|mul\((\d+),(\d+)\)"#).unwrap(); + let mut total = 0; + let mut enabled = true; + + for capture in regex.captures_iter(input) { + let instr = capture.get(0).unwrap().as_str(); + + match instr { + "do()" => enabled = true, + "don't()" => enabled = false, + // `mul(a,b)` + _ => { + if !enabled { + continue; + } + + let first: u64 = capture.get(1).unwrap().as_str().parse().unwrap(); + let second: u64 = capture.get(2).unwrap().as_str().parse().unwrap(); + + total += first * second; + } + } + } + + print_res!("Sum of multiplications: {total}"); +} + +pub fn main() -> color_eyre::Result<()> { + let context = load()?; + + let start = Instant::now(); + let parsed = parsing(&context.input)?; + let elapsed = humantime::format_duration(start.elapsed()); + + let start = Instant::now(); + if context.part == 1 { + part1(parsed); + } else { + part2(parsed); + } + let elapsed_part = humantime::format_duration(start.elapsed()); + + println!(" Parsing: {elapsed}"); + println!(" Solving: {elapsed_part}"); + + Ok(()) +}