Day 10 part 2

This commit is contained in:
2023-12-26 17:03:46 +01:00
parent a6a9584a51
commit 01ba0f6662
2 changed files with 81 additions and 15 deletions
+55 -12
View File
@@ -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 {
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<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(())
}