From 19831672529c7b389f60d8c04802117f8079489e Mon Sep 17 00:00:00 2001 From: Lucy Date: Tue, 3 Dec 2024 11:27:45 +0100 Subject: [PATCH] feat: day 3 --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + inputs/day03/example.txt | 1 + inputs/day03/example_2.txt | 1 + src/day01.rs | 2 +- src/day02.rs | 2 +- src/day03.rs | 42 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 10 +++++---- 8 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 inputs/day03/example.txt create mode 100644 inputs/day03/example_2.txt create mode 100644 src/day03.rs diff --git a/Cargo.lock b/Cargo.lock index 25c2b24..379ec3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,11 +17,21 @@ 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 = "aoc2024" version = "0.1.0" dependencies = [ "color-eyre", + "regex", "tracing", "tracing-subscriber", ] @@ -199,6 +209,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 = "rustc-demangle" version = "0.1.24" diff --git a/Cargo.toml b/Cargo.toml index 4f81e20..4d3ca00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] color-eyre = "0.6.3" +regex = "1.11.1" tracing = "0.1.41" tracing-subscriber = "0.3.19" diff --git a/inputs/day03/example.txt b/inputs/day03/example.txt new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/inputs/day03/example.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/inputs/day03/example_2.txt b/inputs/day03/example_2.txt new file mode 100644 index 0000000..b774ec9 --- /dev/null +++ b/inputs/day03/example_2.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 diff --git a/src/day01.rs b/src/day01.rs index 0e8e0c5..ff2dfbf 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -33,7 +33,7 @@ pub fn pt2(input: &str) -> usize { mod tests { use std::fs::read_to_string; - use crate::day01::{pt1, pt2}; + use super::{pt1, pt2}; #[test] pub fn test_pt1() { diff --git a/src/day02.rs b/src/day02.rs index fbf0b92..defc189 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -46,7 +46,7 @@ pub fn pt2(input: &str) -> usize { mod tests { use std::fs::read_to_string; - use crate::day02::{pt1, pt2}; + use super::{pt1, pt2}; #[test] pub fn test_pt1() { diff --git a/src/day03.rs b/src/day03.rs new file mode 100644 index 0000000..8a24af5 --- /dev/null +++ b/src/day03.rs @@ -0,0 +1,42 @@ +use regex::Regex; + +pub fn pt1(input: &str) -> usize { + let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap(); + re.captures_iter(input) + .map(|c| c.extract()) + .map(|(_, [x, y])| x.parse::().unwrap() * y.parse::().unwrap()) + .sum() +} + +pub fn pt2(input: &str) -> usize { + let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap(); + re.captures_iter( + &Regex::new(r"don't\(\).*?do\(\)") + .unwrap() + .replace_all(&input.replace('\n', ""), ""), + ) + .map(|c| c.extract()) + .map(|(_, [x, y])| x.parse::().unwrap() * y.parse::().unwrap()) + .sum() +} + +#[cfg(test)] +mod tests { + use std::fs::read_to_string; + + use super::{pt1, pt2}; + + #[test] + pub fn test_pt1() { + let input = + read_to_string("./inputs/day03/example.txt").expect("Missing example.txt for day03"); + assert_eq!(pt1(&input), 161) + } + #[test] + pub fn test_pt2() { + let input = read_to_string("./inputs/day03/example_2.txt") + .expect("Missing example_2.txt for day03"); + + assert_eq!(pt2(&input), 48) + } +} diff --git a/src/main.rs b/src/main.rs index 4d054bd..d00b4de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,13 @@ use std::{fmt::Display, fs::read_to_string, time::Instant}; mod day01; mod day02; +mod day03; fn main() { color_eyre::install().unwrap(); tracing_subscriber::fmt::init(); - solve_day(01, day01::pt1, day01::pt2); - solve_day(02, day02::pt1, day02::pt2); + solve_day(1, day01::pt1, day01::pt2); + solve_day(2, day02::pt1, day02::pt2); + solve_day(3, day03::pt1, day03::pt2); } fn solve_day(day: u8, part_a: Fa, part_b: Fb) @@ -16,8 +18,8 @@ where Fb: Fn(&str) -> Tb, Tb: Display, { - let input = read_to_string(format!("./inputs/day{:02}/input.txt", day)) - .expect(&format!("Missing input.txt for day{:02}", day)); + let input = read_to_string(format!("./inputs/day{day:02}/input.txt")) + .unwrap_or_else(|_| panic!("Missing input.txt for day{day:02}")); let start = Instant::now(); let result_a = part_a(&input); tracing::info!("[{day:02}] Part A: {result_a} \tin {:?}", start.elapsed());