Finish day-05 part-1

This commit is contained in:
Kristofers Solo 2025-12-05 19:49:35 +02:00
parent 71ac0a4c35
commit 3deee94f8a
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
9 changed files with 219 additions and 0 deletions

15
2025/Cargo.lock generated
View File

@ -160,6 +160,21 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
] ]
[[package]]
name = "day-05"
version = "0.1.0"
dependencies = [
"divan",
"itertools",
"miette",
"nom",
"rstest",
"test-log",
"thiserror",
"tracing",
"tracing-subscriber",
]
[[package]] [[package]]
name = "divan" name = "divan"
version = "0.1.21" version = "0.1.21"

View File

@ -5,6 +5,7 @@ members = [
"day-02", "day-02",
"day-03", "day-03",
"day-04", "day-04",
"day-05",
] ]
default-members = ["day-*"] default-members = ["day-*"]
resolver = "2" resolver = "2"

27
2025/day-05/Cargo.toml Normal file
View File

@ -0,0 +1,27 @@
[package]
name = "day-05"
version = "0.1.0"
edition = "2024"
# 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
thiserror.workspace = true
[dev-dependencies]
divan.workspace = true
rstest.workspace = true
test-log.workspace = true
[[bench]]
name = "day-05-bench"
path = "benches/benchmarks.rs"
harness = false
[lints]
workspace = true

View File

@ -0,0 +1,21 @@
use day_05::*;
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_05::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_05::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
2025/day-05/src/lib.rs Normal file
View File

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

106
2025/day-05/src/part1.rs Normal file
View File

@ -0,0 +1,106 @@
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Id(usize);
impl From<usize> for Id {
fn from(value: usize) -> Self {
Self(value)
}
}
impl FromStr for Id {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let num = s.trim().parse::<usize>().map_err(|e| e.to_string())?;
Ok(Self(num))
}
}
#[derive(Debug, Clone, Copy)]
struct Range {
start: Id,
end: Id,
}
impl Range {
fn contains(&self, x: Id) -> bool {
x >= self.start && x <= self.end
}
}
impl FromStr for Range {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (start_str, end_str) = s.trim().split_once('-').ok_or("`-` not found")?;
let start = start_str.parse::<Id>()?;
let end = end_str.parse::<Id>()?;
Ok(Self { start, end })
}
}
#[derive(Debug, Clone)]
struct DB {
ranges: Vec<Range>,
ids: Vec<Id>,
}
impl DB {
fn contains(&self, x: Id) -> bool {
self.ranges.iter().any(|range| range.contains(x))
}
fn count_fresh(&self) -> usize {
self.ids.iter().filter(|&&id| self.contains(id)).count()
}
}
impl FromStr for DB {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (ranges_section, ids_section) = s
.split_once("\n\n")
.ok_or("No blank line separator found")?;
let ranges = ranges_section
.lines()
.map(Range::from_str)
.collect::<Result<Vec<_>, _>>()?;
let ids = ids_section
.lines()
.map(Id::from_str)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self { ranges, ids })
}
}
#[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn process(input: &str) -> miette::Result<usize> {
let db = DB::from_str(input).unwrap();
Ok(db.count_fresh())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> miette::Result<()> {
let input = "3-5
10-14
16-20
12-18
1
5
8
11
17
32
";
let result = 3;
assert_eq!(process(input)?, result);
Ok(())
}
}

23
2025/day-05/src/part2.rs Normal file
View File

@ -0,0 +1,23 @@
use miette::Result;
#[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
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(())
}
}