mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-12-31 13:42:32 +00:00
Finish day-05 part-1
This commit is contained in:
parent
71ac0a4c35
commit
3deee94f8a
15
2025/Cargo.lock
generated
15
2025/Cargo.lock
generated
@ -160,6 +160,21 @@ dependencies = [
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day-05"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"divan",
|
||||
"itertools",
|
||||
"miette",
|
||||
"nom",
|
||||
"rstest",
|
||||
"test-log",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "divan"
|
||||
version = "0.1.21"
|
||||
|
||||
@ -5,6 +5,7 @@ members = [
|
||||
"day-02",
|
||||
"day-03",
|
||||
"day-04",
|
||||
"day-05",
|
||||
]
|
||||
default-members = ["day-*"]
|
||||
resolver = "2"
|
||||
|
||||
27
2025/day-05/Cargo.toml
Normal file
27
2025/day-05/Cargo.toml
Normal 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
|
||||
21
2025/day-05/benches/benchmarks.rs
Normal file
21
2025/day-05/benches/benchmarks.rs
Normal 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();
|
||||
}
|
||||
12
2025/day-05/src/bin/part1.rs
Normal file
12
2025/day-05/src/bin/part1.rs
Normal 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(())
|
||||
}
|
||||
12
2025/day-05/src/bin/part2.rs
Normal file
12
2025/day-05/src/bin/part2.rs
Normal 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
2
2025/day-05/src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod part1;
|
||||
pub mod part2;
|
||||
106
2025/day-05/src/part1.rs
Normal file
106
2025/day-05/src/part1.rs
Normal 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
23
2025/day-05/src/part2.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user