From 01ba0f666236b5eb6a73b6713d136fd8d3f4dbfd Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Tue, 26 Dec 2023 17:03:46 +0100 Subject: [PATCH] Day 10 part 2 --- day10/src/main.rs | 67 +++++++++++++++++++++++++++++++++++-------- myutils/src/matrix.rs | 29 +++++++++++++++++-- 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/day10/src/main.rs b/day10/src/main.rs index 1cf4294..683cd61 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -60,19 +60,24 @@ impl TryFrom for Tile { } } +impl Tile { + fn char(&self) -> char { + match (self.n, self.e, self.s, self.w) { + (false, true, false, true) => '-', + (true, false, true, false) => '|', + (true, true, false, false) => 'L', + (true, false, false, true) => 'J', + (false, false, true, true) => '7', + (false, true, true, false) => 'F', + (false, false, false, false) => '.', + _ => '?', + } + } +} + impl Display for Tile { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let c = match (self.n, self.e, self.s, self.w) { - (false, true, false, true) => '-', - (true, false, true, false) => '|', - (true, true, false, false) => 'L', - (true, false, false, true) => 'J', - (false, false, true, true) => '7', - (false, true, true, false) => 'F', - (false, false, false, false) => '.', - _ => '?', - }; - write!(f, "{}", c) + write!(f, "{}", self.char()) } } @@ -131,7 +136,21 @@ impl Map { Self { grid, start: self.start } } - fn fill + fn fill(&self) -> Matrix { + let shape = self.grid.shape(); + let mut map = Matrix::constant(false, (shape.0 + 1, shape.1 + 1)); + + for x in 1..shape.0 { + for y in 1..shape.1 { + let cell = &self.grid[x-1][y-1]; + map[x][y] = map[x-1][y-1] ^ cell.n ^ cell.e; + } + } + + map + } + + } @@ -208,5 +227,29 @@ fn main() -> Result<()> { .find_map(|(c, (a,b))| Some(c+1).filter(|_| a == b)).unwrap(); println!("Collide at {len}"); + + let fillmap = map.fill(); + + let mut filled = Matrix::constant('?', map.grid.shape()); + for (x,y) in filled.indices() { + let p: Tile = map.grid[x][y]; + let f = fillmap[x][y]; + + filled[x][y] = if !(p.n || p.e || p.s || p.w) { + if f { 'I' } else { 'O' } + } else { + p.char() + } + } + + println!("Filled:\n{}", filled); + + let surface = filled.elems() + .copied() + .filter(|&e| e == 'I') + .count(); + + println!("Inner surface: {surface}"); + Ok(()) } diff --git a/myutils/src/matrix.rs b/myutils/src/matrix.rs index e0380b3..f640135 100644 --- a/myutils/src/matrix.rs +++ b/myutils/src/matrix.rs @@ -1,7 +1,7 @@ //! A large number of logical games, by virtue of existing on paper, use 2-dimensional structures. //! This module implement packed matrices, without the overhead of supporting multiple dimensions. -use std::ops::{Index, IndexMut}; +use std::{ops::{Index, IndexMut}, fmt::Display}; use thiserror::Error; @@ -57,6 +57,10 @@ impl Matrix { (0..h).flat_map(move |x| (0..w).map(move |y| (x,y))) } + pub fn elems(&self) -> impl Iterator { + self.vec.iter() + } + /// Lists all the neighbors of the given location, truncating at the edge. pub fn neighbors(&self, pos: (usize, usize)) -> Vec<(usize,usize)> { let (x,y) = pos; @@ -92,6 +96,25 @@ impl Matrix { } +impl Matrix { + pub fn constant(item: T, shape: (usize, usize)) -> Self { + let data = vec![item; shape.0 * shape.1]; + Matrix::new(data, shape).unwrap() + } +} + +impl Display for Matrix { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for l in self.lines() { + for e in l { + write!(f, "{}", e)?; + } + writeln!(f)?; + } + Ok(()) + } +} + impl Index for Matrix { type Output = [T]; @@ -109,7 +132,7 @@ impl IndexMut for Matrix { macro_rules! umat { [$e:expr; $shape:expr] => { - $crate::util::matrix::Matrix::new(vec![$e; $shape.0 * $shape.1], $shape).unwrap() + $crate::matrix::Matrix::new(vec![$e; $shape.0 * $shape.1], $shape).unwrap() }; } pub(crate) use umat; @@ -134,7 +157,7 @@ pub(crate) use mat; #[cfg(test)] mod test { - use crate::util::matrix::ShapeError; + use crate::matrix::ShapeError; use super::Matrix;