Finish part-1

This commit is contained in:
Kristofers Solo 2025-12-09 09:05:11 +02:00
parent 838f24ea5d
commit ecdd78ed14
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8
5 changed files with 82 additions and 7 deletions

1
2025/Cargo.lock generated
View File

@ -226,6 +226,7 @@ name = "day-09"
version = "0.1.0"
dependencies = [
"divan",
"glam",
"itertools",
"miette",
"nom",

View File

@ -1,3 +1,4 @@
use miette::miette;
#[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]

View File

@ -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;

View File

@ -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

View File

@ -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<Self, Self::Err> {
let str = s.trim().split(',').collect::<Vec<_>>();
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<usize, String> {
s.parse::<usize>().map_err(|e| e.to_string())
}
#[derive(Debug, Clone)]
struct Grid(Vec<Tile>);
impl FromStr for Grid {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let grid = s
.trim()
.lines()
.map(Tile::from_str)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self(grid))
}
}
impl Grid {
fn get_max_area(&self) -> usize {
self.area_list().max().unwrap_or(0)
}
fn area_list(&self) -> impl Iterator<Item = usize> {
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<usize> {
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(())
}