day-03 part-1

This commit is contained in:
Kristofers Solo 2024-12-03 09:21:02 +02:00
parent 3789ea1005
commit d986d89c78
8 changed files with 170 additions and 1 deletions

17
2024/Cargo.lock generated
View File

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@ -143,6 +143,21 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "day-03"
version = "0.1.0"
dependencies = [
"divan",
"itertools",
"miette",
"nom",
"regex",
"rstest",
"test-log",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "divan"
version = "0.1.16"

24
2024/day-03/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "day-03"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools.workspace = true
nom.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
miette.workspace = true
regex = "1.11"
[dev-dependencies]
divan.workspace = true
rstest.workspace = true
test-log.workspace = true
[[bench]]
name = "day-03-bench"
path = "benches/benchmarks.rs"
harness = false

View File

@ -0,0 +1,21 @@
use day_03::*;
fn main() {
divan::main();
}
#[divan::bench]
fn part1() {
part1::process(divan::black_box(include_str!(
"../input1.txt",
)))
.unwrap();
}
#[divan::bench]
fn part2() {
part2::process(divan::black_box(include_str!(
"../input2.txt",
)))
.unwrap();
}

View File

@ -0,0 +1,12 @@
use day_03::part1::process;
use miette::{Context, Result};
#[tracing::instrument]
fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let file = include_str!("../../input1.txt");
let result = process(file).context("process part 1")?;
println!("{}", result);
Ok(())
}

View File

@ -0,0 +1,12 @@
use day_03::part2::process;
use miette::{Context, Result};
#[tracing::instrument]
fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let file = include_str!("../../input2.txt");
let result = process(file).context("process part 2")?;
println!("{}", result);
Ok(())
}

2
2024/day-03/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod part1;
pub mod part2;

62
2024/day-03/src/part1.rs Normal file
View File

@ -0,0 +1,62 @@
use std::num::ParseIntError;
use miette::Result;
use regex::Regex;
#[derive(Debug)]
struct Multiplication(usize, usize);
impl Multiplication {
fn calculate(&self) -> usize {
self.0 * self.1
}
}
impl<T, U> TryFrom<(T, U)> for Multiplication
where
T: Into<String>,
U: Into<String>,
{
type Error = ParseIntError;
fn try_from(value: (T, U)) -> std::result::Result<Self, Self::Error> {
Ok(Self(
value.0.into().parse::<usize>()?,
value.1.into().parse::<usize>()?,
))
}
}
fn extract_multiplications<'a>(
line: &'a str,
re: &'a Regex,
) -> impl Iterator<Item = Multiplication> + 'a {
re.captures_iter(line).filter_map(|caps| {
let first = caps.get(1)?.as_str().to_owned();
let second = caps.get(2)?.as_str().to_owned();
Multiplication::try_from((first, second)).ok()
})
}
#[tracing::instrument]
pub fn process(input: &str) -> Result<usize> {
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap();
let result = input
.lines()
.flat_map(|line| extract_multiplications(line, &re))
.map(|mult| mult.calculate())
.sum();
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> Result<()> {
let input = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
let result = 161;
assert_eq!(process(input)?, result);
Ok(())
}
}

21
2024/day-03/src/part2.rs Normal file
View File

@ -0,0 +1,21 @@
use miette::Result;
#[tracing::instrument]
pub fn process(input: &str) -> Result<usize> {
todo!("day xx - part 2");
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> Result<()> {
let input = "";
todo!("haven't built test yet");
let result = 0;
assert_eq!(process(input)?, result);
Ok(())
}
}