Day 10 part 2
This commit is contained in:
parent
a6a9584a51
commit
01ba0f6662
@ -60,19 +60,24 @@ impl TryFrom<char> 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 {
|
impl Display for Tile {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let c = match (self.n, self.e, self.s, self.w) {
|
write!(f, "{}", self.char())
|
||||||
(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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +136,21 @@ impl Map {
|
|||||||
Self { grid, start: self.start }
|
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();
|
.find_map(|(c, (a,b))| Some(c+1).filter(|_| a == b)).unwrap();
|
||||||
|
|
||||||
println!("Collide at {len}");
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! A large number of logical games, by virtue of existing on paper, use 2-dimensional structures.
|
//! 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.
|
//! 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;
|
use thiserror::Error;
|
||||||
|
|
||||||
@ -57,6 +57,10 @@ impl <T> Matrix<T> {
|
|||||||
(0..h).flat_map(move |x| (0..w).map(move |y| (x,y)))
|
(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.
|
/// Lists all the neighbors of the given location, truncating at the edge.
|
||||||
pub fn neighbors(&self, pos: (usize, usize)) -> Vec<(usize,usize)> {
|
pub fn neighbors(&self, pos: (usize, usize)) -> Vec<(usize,usize)> {
|
||||||
let (x,y) = pos;
|
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> {
|
impl <T> Index<usize> for Matrix<T> {
|
||||||
type Output = [T];
|
type Output = [T];
|
||||||
|
|
||||||
@ -109,7 +132,7 @@ impl <T> IndexMut<usize> for Matrix<T> {
|
|||||||
|
|
||||||
macro_rules! umat {
|
macro_rules! umat {
|
||||||
[$e:expr; $shape:expr] => {
|
[$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;
|
pub(crate) use umat;
|
||||||
@ -134,7 +157,7 @@ pub(crate) use mat;
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::util::matrix::ShapeError;
|
use crate::matrix::ShapeError;
|
||||||
|
|
||||||
use super::Matrix;
|
use super::Matrix;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user