Finish day-11 part-1

This commit is contained in:
Kristofers Solo 2025-12-11 20:55:46 +02:00
parent d236d75bf4
commit a2ccf9b271
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
9 changed files with 220 additions and 0 deletions

15
2025/Cargo.lock generated
View File

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

View File

@ -11,6 +11,7 @@ members = [
"day-08",
"day-09",
"day-10",
"day-11",
]
default-members = ["day-*"]
resolver = "2"

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

@ -0,0 +1,27 @@
[package]
name = "day-11"
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-11-bench"
path = "benches/benchmarks.rs"
harness = false
[lints]
workspace = true

View File

@ -0,0 +1,15 @@
use day_11::{part1, part2};
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_11::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_11::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-11/src/lib.rs Normal file
View File

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

113
2025/day-11/src/part1.rs Normal file
View File

@ -0,0 +1,113 @@
use itertools::Itertools;
use miette::miette;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq)]
struct Name([char; 3]);
impl Name {
const YOU: Self = Self(['y', 'o', 'u']);
const OUT: Self = Self(['o', 'u', 't']);
}
impl FromStr for Name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let chars = s.trim().chars().collect_vec();
if chars.len() != 3 {
return Err(format!(
"Name must be exactly 3 characters, got {}",
chars.len()
));
}
Ok(Self([chars[0], chars[1], chars[2]]))
}
}
#[derive(Debug, Clone)]
struct Device {
input: Name,
outputs: Vec<Name>,
}
impl FromStr for Device {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let trimmed = s.trim();
let (input_str, output_str) = trimmed.split_once(':').ok_or("Missing `:`")?;
Ok(Self {
input: Name::from_str(input_str)?,
outputs: output_str
.split_whitespace()
.map(Name::from_str)
.collect::<Result<Vec<_>, _>>()?,
})
}
}
#[derive(Debug, Clone)]
struct Rack(Vec<Device>);
impl FromStr for Rack {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let rack = s
.lines()
.map(Device::from_str)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self(rack))
}
}
impl Rack {
fn solve(&self) -> usize {
dfs(&Name::YOU, &self.0)
}
}
fn dfs(current: &Name, devices: &[Device]) -> usize {
if current == &Name::OUT {
return 1;
}
devices
.iter()
.find(|d| d.input == *current)
.map_or(0, |device| {
device
.outputs
.iter()
.map(|output| dfs(output, devices))
.sum()
})
}
#[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn process(input: &str) -> miette::Result<usize> {
let rack = Rack::from_str(input).map_err(|e| miette!("{e}"))?;
Ok(rack.solve())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> miette::Result<()> {
let input = "aaa: you hhh
you: bbb ccc
bbb: ddd eee
ccc: ddd eee fff
ddd: ggg
eee: out
fff: out
ggg: out
hhh: ccc fff iii
iii: out";
let result = 5;
assert_eq!(process(input)?, result);
Ok(())
}
}

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

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