diff --git a/Cargo.lock b/Cargo.lock index d90c668..230dadf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -43,6 +52,7 @@ dependencies = [ "colored", "lazy_static", "mut_static", + "regex", "reqwest", ] @@ -837,6 +847,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 = "reqwest" version = "0.11.27" diff --git a/Cargo.toml b/Cargo.toml index 3609d01..4e2fb1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ aoc-macro = {path="aoc-macro"} reqwest = { version = "0.11", features=["cookies", "blocking"] } mut_static="5.0" lazy_static="1.4" +regex = "1.11" [profile.release] opt-level = 3 diff --git a/README.md b/README.md index 6d57704..5b0b40b 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ [![About](https://img.shields.io/badge/Advent%20of%20Code-2024-brightgreen?style=flat-square)](https://adventofcode.com/2024/about) [![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language)) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/) -![Days completed](https://img.shields.io/badge/Days%20completed-1-red?style=flat-square) -![Stars](https://img.shields.io/badge/Stars-3-yellow?style=flat-square) +![Days completed](https://img.shields.io/badge/Days%20completed-2.5-red?style=flat-square) +![Stars](https://img.shields.io/badge/Stars-5-yellow?style=flat-square) > ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet. diff --git a/src/days/d03.rs b/src/days/d03.rs index 59c0ad7..3a06482 100644 --- a/src/days/d03.rs +++ b/src/days/d03.rs @@ -1,32 +1,88 @@ use super::{Answer, Day, DayImpl}; +use regex::Regex; const CURRENT_DAY: u8 = 3; -type Data = Vec; +#[derive(Clone, Debug)] +pub struct Command { + command: String, + parameters: Vec, +} + +impl Command { + pub fn run(&self) -> u64 { + if !self.command.ends_with("mul") || self.parameters.len() < 2 { + return 0; + } + self.parameters[0] * self.parameters[1] + } +} + +#[derive(Clone, Debug)] +pub struct Program(Vec); + +impl Program { + pub fn do_all_multiplications(&self) -> u64 { + self.0.iter().map(|v| v.run()).sum() + } + + pub fn run(&self) -> u64 { + let mut enabled = true; + + self.0 + .iter() + .map(|v| { + if v.command.ends_with("don't") { + enabled = false; + } else if v.command.ends_with("do") { + enabled = true; + } else if enabled { + return v.run(); + } + 0 + }) + .sum() + } +} + +impl From<&str> for Program { + fn from(value: &str) -> Self { + let pattern: Regex = Regex::new(r"([a-z_']+)\(((?:\d+,?)*)\)").unwrap(); + + let mut commands = vec![]; + for (_, [command, parameters]) in pattern.captures_iter(value).map(|c| c.extract()) { + commands.push(Command { + command: command.to_string(), + parameters: parameters + .split(",") + .map(|v| v.parse().unwrap_or(0)) + .collect(), + }); + } + + Self(commands) + } +} + +type Data = Program; impl DayImpl for Day { fn init_test() -> (Self, Data) { Self::init(include_str!("test_inputs/test03.txt")) } fn expected_results() -> (Answer, Answer) { - (Answer::Number(0), Answer::Number(0)) + (Answer::Number(161), Answer::Number(0)) } fn init(input: &str) -> (Self, Data) { - ( - Self {}, - input - .lines() - .map(|v| v.parse::().expect("error while parsing input.")) - .collect(), - ) + (Self {}, input.into()) } fn one(&self, data: &mut Data) -> Answer { - Answer::Number(data.len() as u64) + Answer::Number(data.do_all_multiplications()) } fn two(&self, data: &mut Data) -> Answer { - Answer::Number(data.len() as u64) + Answer::Number(data.run()) } } diff --git a/src/days/test_inputs/test03.txt b/src/days/test_inputs/test03.txt index e69de29..b774ec9 100644 --- a/src/days/test_inputs/test03.txt +++ b/src/days/test_inputs/test03.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) \ No newline at end of file