aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit 433efb35ed49be9f2a05e7b8bf2d1cc3987b6bb3
parent 19b7ee46b55ed25f77fd030ccc473c8cf95e07b5
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Fri,  4 Jul 2025 16:45:46 +0200

Day 18 2022

Diffstat:
A2022/18/a.rs | 7+++++++
A2022/18/b.rs | 10++++++++++
A2022/18/common.rs | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 112 insertions(+), 0 deletions(-)

diff --git a/2022/18/a.rs b/2022/18/a.rs @@ -0,0 +1,7 @@ +mod common; +use common::*; + +fn main() { + let map = Map::from_stdin(); + println!("{}", map.total_free_area()); +} diff --git a/2022/18/b.rs b/2022/18/b.rs @@ -0,0 +1,10 @@ +mod common; +use common::*; + +fn main() { + let mut map = Map::from_stdin(); + let total_free = map.total_free_area(); + map.fill_exterior(); + let interior_free = map.total_free_area() - 6*MAX_LEN*MAX_LEN; + println!("{}", total_free - interior_free); +} diff --git a/2022/18/common.rs b/2022/18/common.rs @@ -0,0 +1,95 @@ +pub const MAX_LEN: usize = 20; +pub const DIR: [(i8, i8, i8); 6] = [ + (1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1) +]; + +fn inb(i: i8, j: i8, k: i8) -> bool { + i >= 0 && i < MAX_LEN as i8 && + j >= 0 && j < MAX_LEN as i8 && + k >= 0 && k < MAX_LEN as i8 +} + +pub struct Map { + cell: [[[bool; MAX_LEN]; MAX_LEN]; MAX_LEN] +} + +impl Map { + pub fn new() -> Map { + Map { cell: [[[false; MAX_LEN]; MAX_LEN]; MAX_LEN] } + } + + pub fn from_stdin() -> Map { + let mut map = Map::new(); + let mut line = String::new(); + while std::io::stdin().read_line(&mut line).unwrap() > 0 { + let i = 0; + let j = line.find(',').unwrap(); + let x = line[i..j].parse::<usize>().unwrap(); + let i = j+1; + let j = line[i..].find(',').unwrap() + i; + let y = line[i..j].parse::<usize>().unwrap(); + let i = j+1; + let j = line.find('\n').unwrap(); + let z = line[i..j].parse::<usize>().unwrap(); + map.set(x, y, z, true); + line.clear(); + } + map + } + + pub fn get(&self, x: i8, y: i8, z: i8) -> bool { + inb(x, y, z) && self.cell[x as usize][y as usize][z as usize] + } + + pub fn set(&mut self, x: usize, y: usize, z: usize, b: bool) { + self.cell[x][y][z] = b + } + + fn free_area(&self, x: usize, y: usize, z: usize) -> usize { + let mut r = 0; + for d in DIR { + let (i, j, k) = (x as i8 + d.0, y as i8 + d.1, z as i8 + d.2); + if !self.get(i, j, k) { + r += 1 + } + } + r + } + + pub fn total_free_area(&self) -> usize { + let mut r = 0; + for i in 0..MAX_LEN { + for j in 0..MAX_LEN { + for k in 0..MAX_LEN { + if self.get(i as i8, j as i8, k as i8) { + r += self.free_area(i, j, k); + } + } + } + } + r + } + + #[allow(dead_code)] + fn fill_from(&mut self, x: i8, y: i8, z: i8) { + if !inb(x, y, z) || self.get(x, y, z) { return; } + self.set(x as usize, y as usize, z as usize, true); + for d in DIR { + self.fill_from(x + d.0, y + d.1, z + d.2); + } + } + + #[allow(dead_code)] + pub fn fill_exterior(&mut self) { + for i in 0..MAX_LEN { + for j in 0..MAX_LEN { + self.fill_from(0 as i8, i as i8, j as i8); + self.fill_from((MAX_LEN-1) as i8, i as i8, j as i8); + self.fill_from(i as i8, 0 as i8, j as i8); + self.fill_from(i as i8, (MAX_LEN-1) as i8, j as i8); + self.fill_from(i as i8, j as i8, 0 as i8); + self.fill_from(i as i8, j as i8, (MAX_LEN-1) as i8); + } + } + } +}