From ecdd78ed142e70ca5b455edb9be58733454a0af2 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 9 Dec 2025 09:05:11 +0200 Subject: [PATCH] Finish part-1 --- 2025/Cargo.lock | 1 + 2025/daily-template/src/part1.rs | 1 + 2025/daily-template/src/part2.rs | 4 +- 2025/day-09/Cargo.toml | 1 + 2025/day-09/src/part1.rs | 82 ++++++++++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 7 deletions(-) diff --git a/2025/Cargo.lock b/2025/Cargo.lock index a54b8a6..831a3c3 100644 --- a/2025/Cargo.lock +++ b/2025/Cargo.lock @@ -226,6 +226,7 @@ name = "day-09" version = "0.1.0" dependencies = [ "divan", + "glam", "itertools", "miette", "nom", diff --git a/2025/daily-template/src/part1.rs b/2025/daily-template/src/part1.rs index 1639d01..044838f 100644 --- a/2025/daily-template/src/part1.rs +++ b/2025/daily-template/src/part1.rs @@ -1,3 +1,4 @@ +use miette::miette; #[tracing::instrument] #[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_errors_doc)] diff --git a/2025/daily-template/src/part2.rs b/2025/daily-template/src/part2.rs index ede34a7..ef3e35b 100644 --- a/2025/daily-template/src/part2.rs +++ b/2025/daily-template/src/part2.rs @@ -1,4 +1,4 @@ -use miette::Result; +use miette::miette; #[tracing::instrument] #[allow(clippy::missing_panics_doc)] @@ -13,7 +13,7 @@ mod tests { use super::*; #[test] - fn test_process() -> Result<()> { + fn test_process() -> miette::Result<()> { let input = ""; todo!("haven't built test yet"); let result = 0; diff --git a/2025/day-09/Cargo.toml b/2025/day-09/Cargo.toml index 95d33be..32ccb04 100644 --- a/2025/day-09/Cargo.toml +++ b/2025/day-09/Cargo.toml @@ -12,6 +12,7 @@ tracing.workspace = true tracing-subscriber.workspace = true miette.workspace = true thiserror.workspace = true +glam.workspace = true [dev-dependencies] divan.workspace = true diff --git a/2025/day-09/src/part1.rs b/2025/day-09/src/part1.rs index 1639d01..8de4cee 100644 --- a/2025/day-09/src/part1.rs +++ b/2025/day-09/src/part1.rs @@ -1,9 +1,75 @@ +use glam::{USizeVec2, usize}; +use miette::miette; +use std::str::FromStr; + +#[derive(Debug, Clone, Copy)] +struct Tile(USizeVec2); + +impl Tile { + const fn x(&self) -> usize { + self.0.x + } + + const fn y(&self) -> usize { + self.0.y + } + + const fn area(&self, other: Self) -> usize { + (self.x().abs_diff(other.x()) + 1) * (self.y().abs_diff(other.y()) + 1) + } +} + +impl FromStr for Tile { + type Err = String; + fn from_str(s: &str) -> Result { + let str = s.trim().split(',').collect::>(); + if str.len() < 2 { + return Err("Missing coords".to_string()); + } + Ok(Self(USizeVec2 { + x: str_to_usize(str[0])?, + y: str_to_usize(str[1])?, + })) + } +} + +fn str_to_usize(s: &str) -> Result { + s.parse::().map_err(|e| e.to_string()) +} + +#[derive(Debug, Clone)] +struct Grid(Vec); + +impl FromStr for Grid { + type Err = String; + fn from_str(s: &str) -> Result { + let grid = s + .trim() + .lines() + .map(Tile::from_str) + .collect::, _>>()?; + Ok(Self(grid)) + } +} + +impl Grid { + fn get_max_area(&self) -> usize { + self.area_list().max().unwrap_or(0) + } + + fn area_list(&self) -> impl Iterator { + self.0 + .iter() + .flat_map(|&a| self.0.iter().map(move |&b| a.area(b))) + } +} + #[tracing::instrument] #[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_errors_doc)] pub fn process(input: &str) -> miette::Result { - todo!("day xx - part 1"); - Ok(0) + let grid = Grid::from_str(input).map_err(|e| miette!("{e}"))?; + Ok(grid.get_max_area()) } #[cfg(test)] @@ -12,9 +78,15 @@ mod tests { #[test] fn test_process() -> miette::Result<()> { - let input = ""; - todo!("haven't built test yet"); - let result = 0; + let input = "7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3"; + let result = 50; assert_eq!(process(input)?, result); Ok(()) }