feat: day 3

This commit is contained in:
Lucy 2024-12-03 11:27:45 +01:00
parent 5b722a30d6
commit 1983167252
8 changed files with 92 additions and 6 deletions

39
Cargo.lock generated
View file

@ -17,11 +17,21 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 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]] [[package]]
name = "aoc2024" name = "aoc2024"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"color-eyre", "color-eyre",
"regex",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]
@ -199,6 +209,35 @@ dependencies = [
"proc-macro2", "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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"

View file

@ -8,5 +8,6 @@ edition = "2021"
[dependencies] [dependencies]
color-eyre = "0.6.3" color-eyre = "0.6.3"
regex = "1.11.1"
tracing = "0.1.41" tracing = "0.1.41"
tracing-subscriber = "0.3.19" tracing-subscriber = "0.3.19"

1
inputs/day03/example.txt Normal file
View file

@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

View file

@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

View file

@ -33,7 +33,7 @@ pub fn pt2(input: &str) -> usize {
mod tests { mod tests {
use std::fs::read_to_string; use std::fs::read_to_string;
use crate::day01::{pt1, pt2}; use super::{pt1, pt2};
#[test] #[test]
pub fn test_pt1() { pub fn test_pt1() {

View file

@ -46,7 +46,7 @@ pub fn pt2(input: &str) -> usize {
mod tests { mod tests {
use std::fs::read_to_string; use std::fs::read_to_string;
use crate::day02::{pt1, pt2}; use super::{pt1, pt2};
#[test] #[test]
pub fn test_pt1() { pub fn test_pt1() {

42
src/day03.rs Normal file
View file

@ -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::<usize>().unwrap() * y.parse::<usize>().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::<usize>().unwrap() * y.parse::<usize>().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)
}
}

View file

@ -2,11 +2,13 @@ use std::{fmt::Display, fs::read_to_string, time::Instant};
mod day01; mod day01;
mod day02; mod day02;
mod day03;
fn main() { fn main() {
color_eyre::install().unwrap(); color_eyre::install().unwrap();
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
solve_day(01, day01::pt1, day01::pt2); solve_day(1, day01::pt1, day01::pt2);
solve_day(02, day02::pt1, day02::pt2); solve_day(2, day02::pt1, day02::pt2);
solve_day(3, day03::pt1, day03::pt2);
} }
fn solve_day<Fa, Fb, Ta, Tb>(day: u8, part_a: Fa, part_b: Fb) fn solve_day<Fa, Fb, Ta, Tb>(day: u8, part_a: Fa, part_b: Fb)
@ -16,8 +18,8 @@ where
Fb: Fn(&str) -> Tb, Fb: Fn(&str) -> Tb,
Tb: Display, Tb: Display,
{ {
let input = read_to_string(format!("./inputs/day{:02}/input.txt", day)) let input = read_to_string(format!("./inputs/day{day:02}/input.txt"))
.expect(&format!("Missing input.txt for day{:02}", day)); .unwrap_or_else(|_| panic!("Missing input.txt for day{day:02}"));
let start = Instant::now(); let start = Instant::now();
let result_a = part_a(&input); let result_a = part_a(&input);
tracing::info!("[{day:02}] Part A: {result_a} \tin {:?}", start.elapsed()); tracing::info!("[{day:02}] Part A: {result_a} \tin {:?}", start.elapsed());