Day 10 part 2
This commit is contained in:
parent
a6a9584a51
commit
01ba0f6662
@ -60,9 +60,9 @@ impl TryFrom<char> for Tile {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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',
|
||||
@ -71,8 +71,13 @@ impl Display for Tile {
|
||||
(false, true, true, false) => 'F',
|
||||
(false, false, false, false) => '.',
|
||||
_ => '?',
|
||||
};
|
||||
write!(f, "{}", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Tile {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.char())
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,7 +136,21 @@ impl Map {
|
||||
Self { grid, start: self.start }
|
||||
}
|
||||
|
||||
fn fill
|
||||
fn fill(&self) -> Matrix<bool> {
|
||||
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(())
|
||||
}
|
||||
|
@ -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 <T> Matrix<T> {
|
||||
(0..h).flat_map(move |x| (0..w).map(move |y| (x,y)))
|
||||
}
|
||||
|
||||
pub fn elems(&self) -> impl Iterator<Item=&T> {
|
||||
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 <T> Matrix<T> {
|
||||
|
||||
}
|
||||
|
||||
impl <T: Clone> Matrix<T> {
|
||||
pub fn constant(item: T, shape: (usize, usize)) -> Self {
|
||||
let data = vec![item; shape.0 * shape.1];
|
||||
Matrix::new(data, shape).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl <T: Display> Display for Matrix<T> {
|
||||
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 <T> Index<usize> for Matrix<T> {
|
||||
type Output = [T];
|
||||
|
||||
@ -109,7 +132,7 @@ impl <T> IndexMut<usize> for Matrix<T> {
|
||||
|
||||
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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user