mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-12-31 13:42:32 +00:00
Finish part-1
This commit is contained in:
parent
838f24ea5d
commit
ecdd78ed14
1
2025/Cargo.lock
generated
1
2025/Cargo.lock
generated
@ -226,6 +226,7 @@ name = "day-09"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"divan",
|
"divan",
|
||||||
|
"glam",
|
||||||
"itertools",
|
"itertools",
|
||||||
"miette",
|
"miette",
|
||||||
"nom",
|
"nom",
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use miette::miette;
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
#[allow(clippy::missing_panics_doc)]
|
#[allow(clippy::missing_panics_doc)]
|
||||||
#[allow(clippy::missing_errors_doc)]
|
#[allow(clippy::missing_errors_doc)]
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use miette::Result;
|
use miette::miette;
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
#[allow(clippy::missing_panics_doc)]
|
#[allow(clippy::missing_panics_doc)]
|
||||||
@ -13,7 +13,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_process() -> Result<()> {
|
fn test_process() -> miette::Result<()> {
|
||||||
let input = "";
|
let input = "";
|
||||||
todo!("haven't built test yet");
|
todo!("haven't built test yet");
|
||||||
let result = 0;
|
let result = 0;
|
||||||
|
|||||||
@ -12,6 +12,7 @@ tracing.workspace = true
|
|||||||
tracing-subscriber.workspace = true
|
tracing-subscriber.workspace = true
|
||||||
miette.workspace = true
|
miette.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
|
glam.workspace = true
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
divan.workspace = true
|
divan.workspace = true
|
||||||
|
|||||||
@ -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]
|
#[tracing::instrument]
|
||||||
#[allow(clippy::missing_panics_doc)]
|
#[allow(clippy::missing_panics_doc)]
|
||||||
#[allow(clippy::missing_errors_doc)]
|
#[allow(clippy::missing_errors_doc)]
|
||||||
pub fn process(input: &str) -> miette::Result<usize> {
|
pub fn process(input: &str) -> miette::Result<usize> {
|
||||||
todo!("day xx - part 1");
|
let grid = Grid::from_str(input).map_err(|e| miette!("{e}"))?;
|
||||||
Ok(0)
|
Ok(grid.get_max_area())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -12,9 +78,15 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_process() -> miette::Result<()> {
|
fn test_process() -> miette::Result<()> {
|
||||||
let input = "";
|
let input = "7,1
|
||||||
todo!("haven't built test yet");
|
11,1
|
||||||
let result = 0;
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3";
|
||||||
|
let result = 50;
|
||||||
assert_eq!(process(input)?, result);
|
assert_eq!(process(input)?, result);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user