Finish day-04 part1

This commit is contained in:
Kristofers Solo 2025-12-04 11:56:55 +02:00
parent 3cfc40324c
commit 4a259655e2
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8
9 changed files with 247 additions and 0 deletions

15
2025/Cargo.lock generated
View File

@ -145,6 +145,21 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "day-04"
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

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

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

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

View File

@ -0,0 +1,21 @@
use day_04::*;
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_04::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_04::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-04/src/lib.rs Normal file
View File

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

134
2025/day-04/src/part1.rs Normal file
View File

@ -0,0 +1,134 @@
use std::{convert::Infallible, str::FromStr};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Space {
Roll,
Empty,
}
impl From<char> for Space {
fn from(ch: char) -> Self {
match ch {
'@' => Self::Roll,
_ => Self::Empty,
}
}
}
#[derive(Debug, Clone)]
struct Row(Vec<Space>);
impl FromStr for Row {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let row = s.chars().map(Space::from).collect();
Ok(Self(row))
}
}
#[derive(Debug, Clone)]
struct Grid(Vec<Row>);
impl FromStr for Grid {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let grid = s
.lines()
.map(Row::from_str)
.collect::<Result<Vec<_>, Infallible>>()
.unwrap();
Ok(Self(grid))
}
}
#[derive(Debug)]
struct Coords {
x: isize,
y: isize,
}
impl Coords {
const fn new(x: isize, y: isize) -> Self {
Self { x, y }
}
const fn offset(&self, coords: &Self) -> Self {
Self {
x: self.x + coords.x,
y: self.y + coords.y,
}
}
}
impl Grid {
fn find_accessible(&self) -> usize {
self.0
.iter()
.enumerate()
.flat_map(|(x, row)| row.0.iter().enumerate().map(move |(y, _)| (x, y)))
.filter(|(x, y)| {
let coords = Coords::new((*x).try_into().unwrap(), (*y).try_into().unwrap());
let adjacent = self.get_adjacent(&coords);
adjacent < 4 && matches!(self.get(&coords), Space::Roll)
})
.count()
}
fn get_adjacent(&self, coords: &Coords) -> usize {
let max_x = self.0.len() - 1;
let max_y = self.0[0].0.len() - 1;
(-1..=1)
.flat_map(|x| (-1..=1).map(move |y| (x, y)))
.filter(|(x, y)| {
!(*x == 0 && *y == 0)
&& !((coords.x == 0 && *x == -1)
|| (coords.y == 0 && *y == -1)
|| (usize::try_from(coords.x).unwrap() == max_x && *x == 1)
|| (usize::try_from(coords.y).unwrap() == max_y && *y == 1))
})
.filter(|(x, y)| {
let coord = coords.offset(&Coords::new(
(*x).try_into().unwrap(),
(*y).try_into().unwrap(),
));
matches!(self.get(&coord), Space::Roll)
})
.count()
}
fn get(&self, coords: &Coords) -> Space {
self.0[usize::try_from(coords.x).unwrap()].0[usize::try_from(coords.y).unwrap()]
}
}
#[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn process(input: &str) -> miette::Result<usize> {
let grid = Grid::from_str(input)?;
Ok(grid.find_accessible())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> miette::Result<()> {
let input = "..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
";
let result = 13;
assert_eq!(process(input)?, result);
Ok(())
}
}

23
2025/day-04/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(())
}
}