aoc

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

common.rs (2832B)


      1 pub const MAX_LEN: usize = 20;
      2 pub const DIR: [(i8, i8, i8); 6] = [
      3     (1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)
      4 ];
      5 
      6 fn inb(i: i8, j: i8, k: i8) -> bool {
      7     i >= 0 && i < MAX_LEN as i8 &&
      8     j >= 0 && j < MAX_LEN as i8 &&
      9     k >= 0 && k < MAX_LEN as i8
     10 }
     11 
     12 pub struct Map {
     13     cell: [[[bool; MAX_LEN]; MAX_LEN]; MAX_LEN]
     14 }
     15 
     16 impl Map {
     17     pub fn new() -> Map {
     18         Map { cell: [[[false; MAX_LEN]; MAX_LEN]; MAX_LEN] }
     19     }
     20 
     21     pub fn from_stdin() -> Map {
     22         let mut map = Map::new();
     23         let mut line = String::new();
     24         while std::io::stdin().read_line(&mut line).unwrap() > 0 {
     25             let i = 0;
     26             let j = line.find(',').unwrap();
     27             let x = line[i..j].parse::<usize>().unwrap();
     28             let i = j+1;
     29             let j = line[i..].find(',').unwrap() + i;
     30             let y = line[i..j].parse::<usize>().unwrap();
     31             let i = j+1;
     32             let j = line.find('\n').unwrap();
     33             let z = line[i..j].parse::<usize>().unwrap();
     34             map.set(x, y, z, true);
     35             line.clear();
     36         }
     37         map
     38     }
     39 
     40     pub fn get(&self, x: i8, y: i8, z: i8) -> bool {
     41         inb(x, y, z) && self.cell[x as usize][y as usize][z as usize]
     42     }
     43 
     44     pub fn set(&mut self, x: usize, y: usize, z: usize, b: bool) {
     45         self.cell[x][y][z] = b
     46     }
     47 
     48     fn free_area(&self, x: usize, y: usize, z: usize) -> usize {
     49         let mut r = 0;
     50         for d in DIR {
     51             let (i, j, k) = (x as i8 + d.0, y as i8 + d.1, z as i8 + d.2);
     52             if !self.get(i, j, k) {
     53                 r += 1
     54             }
     55         }
     56         r
     57     }
     58 
     59     pub fn total_free_area(&self) -> usize {
     60         let mut r = 0;
     61         for i in 0..MAX_LEN {
     62             for j in 0..MAX_LEN {
     63                 for k in 0..MAX_LEN {
     64                     if self.get(i as i8, j as i8, k as i8) {
     65                         r += self.free_area(i, j, k);
     66                     }
     67                 }
     68             }
     69         }
     70         r
     71     }
     72 
     73     #[allow(dead_code)]
     74     fn fill_from(&mut self, x: i8, y: i8, z: i8) {
     75         if !inb(x, y, z) || self.get(x, y, z) { return; }
     76         self.set(x as usize, y as usize, z as usize, true);
     77         for d in DIR {
     78             self.fill_from(x + d.0, y + d.1, z + d.2);
     79         }
     80     }
     81 
     82     #[allow(dead_code)]
     83     pub fn fill_exterior(&mut self) {
     84         for i in 0..MAX_LEN {
     85             for j in 0..MAX_LEN {
     86                 self.fill_from(0 as i8, i as i8, j as i8);
     87                 self.fill_from((MAX_LEN-1) as i8, i as i8, j as i8);
     88                 self.fill_from(i as i8, 0 as i8, j as i8);
     89                 self.fill_from(i as i8, (MAX_LEN-1) as i8, j as i8);
     90                 self.fill_from(i as i8, j as i8, 0 as i8);
     91                 self.fill_from(i as i8, j as i8, (MAX_LEN-1) as i8);
     92             }
     93         }
     94     }
     95 }