ElTriunfador

Dec 17

Dec 21st, 2020
908
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::fs::File;
  2. use std::io::{BufRead, BufReader};
  3. use std::collections::HashMap;
  4.  
  5. struct Grid {
  6.     grid: HashMap<Vec<i32>,char>,
  7.     resolution: i32,
  8. }
  9.  
  10. impl Grid {
  11.  
  12.     fn initialize(&mut self) {
  13.         self.grid.reserve(self.resolution as usize * self.resolution as usize
  14.             * self.resolution as usize * self.resolution as usize);
  15.     }
  16.  
  17.     fn get(&self, index: &Vec<i32>) -> Option<char> {
  18.         let tmp_index = index.clone();
  19.         if self.grid.contains_key(&tmp_index){
  20.             return Some(self.grid[&tmp_index]);
  21.         }
  22.         return None;
  23.     }
  24.  
  25.     fn set(&mut self, index: &Vec<i32>, val: char) {
  26.         let key = index.clone();
  27.         self.grid.insert(key, val);
  28.     }
  29.  
  30.     fn get_voxels_to_fill(&self) -> Vec<Vec<i32>> {
  31.         let empty_voxels = Grid::get_empty_neighbours(self);
  32.         let mut voxels_to_fill: Vec<Vec<i32>> = Vec::new();
  33.         for empty_index in empty_voxels {
  34.             let nbr_neighbours =  Grid::count_neighbours(self, &empty_index.0);
  35.             if nbr_neighbours == 3 {
  36.                 voxels_to_fill.push(empty_index.0);
  37.             }
  38.         }
  39.         return voxels_to_fill;
  40.     }
  41.  
  42.     fn get_empty_neighbours(&self) -> HashMap<Vec<i32>, Vec<i32>> {
  43.         let mut neighbours: HashMap<Vec<i32>, Vec<i32>> = HashMap::new();
  44.         for (key, _) in &self.grid {
  45.             Grid::append_empty_neighbours(self, &mut neighbours, key);
  46.         }
  47.         return neighbours;
  48.     }
  49.  
  50.     fn append_empty_neighbours(&self, neighbours: &mut HashMap<Vec<i32>, Vec<i32>>, key: &Vec<i32>) {
  51.         let interval: Vec<i32> = vec![-1,0,1];
  52.         for delta_i in &interval {
  53.             for delta_j in &interval {
  54.                 for delta_k in &interval {
  55.                     for delta_w in &interval {
  56.                         if *delta_i == 0 && *delta_j == 0 && *delta_k == 0 && *delta_w == 0 {
  57.                             continue;
  58.                         }
  59.                         let i = key[0] + delta_i;
  60.                         let j = key[1] + delta_j;
  61.                         let k = key[2] + delta_k;
  62.                         let w = key[3] + delta_w;
  63.                         let new_key = vec![i, j, k, w];
  64.                         if Grid::get(self, &new_key) == None {
  65.                             neighbours.insert(new_key.clone(), new_key.clone());
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     fn active_to_keep(&mut self) {
  74.         let mut index_to_remove: Vec<Vec<i32>> = Vec::new();
  75.         for (key, _) in &self.grid {
  76.             let nbr_neighbours = Grid::count_neighbours(self, key);
  77.             if nbr_neighbours == 2 || nbr_neighbours == 3 {
  78.                 continue;
  79.             }
  80.             index_to_remove.push(key.clone());
  81.         }
  82.         for index in index_to_remove {
  83.             self.grid.remove(&index);
  84.         }
  85.     }
  86.  
  87.     fn count_neighbours(&self, key: &Vec<i32>) -> i32 {
  88.         let offset = vec![-1, 0, 1];
  89.         let mut sum = 0;
  90.         for delta_i in &offset {
  91.             for delta_j in &offset {
  92.                 for delta_k in &offset {
  93.                     for delta_w in &offset {
  94.                         if *delta_i == 0 && *delta_j == 0 && *delta_k == 0 && *delta_w == 0 {
  95.                             continue;
  96.                         }
  97.                         let i = key[0] + delta_i;
  98.                         let j = key[1] + delta_j;
  99.                         let k = key[2] + delta_k;
  100.                         let w = key[3] + delta_w;
  101.                         let new_key: Vec<i32> = vec![i, j, k, w];
  102.                         if Grid::get(self, &new_key) == None {
  103.                             continue;
  104.                         }
  105.                         sum += 1;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         return sum;
  111.     }
  112.  
  113.     fn print_grid(&self) {
  114.         let mut sum = 0;
  115.         for (key, value) in &self.grid {
  116.             println!("key: {} {} {} value: {}", key[0], key[1], key[2], value);
  117.             sum += 0;
  118.         }
  119.         println!("\n \n ---------  Length {} ----------", self.grid.len());
  120.         println!("\n {} \n", sum);
  121.     }
  122. }
  123.  
  124. fn main() {
  125.     let board = get_board
  126.         (&String::from("/mnt/c/Users/bylow/Downloads/input-dec17.txt"));
  127.     let nbr_active = get_nbr_active(&board);
  128.     println!("nbr_active: {}", nbr_active);
  129. }
  130.  
  131. fn get_nbr_active(board: &Vec<Vec<char>>) -> usize {
  132.     let mut grid = create_grid(board);
  133.     grid.print_grid();
  134.  
  135.     for _ in 0..6 {
  136.         update_board(&mut grid);
  137.     }
  138.     println!("After update: \n");
  139.     grid.print_grid();
  140.  
  141.     return 0;
  142. }
  143.  
  144. fn create_grid(board: &Vec<Vec<char>>) -> Grid {
  145.     let v: HashMap<Vec<i32>, char> = HashMap::new();
  146.     let mut grid: Grid = Grid{grid: v, resolution: 10};
  147.     grid.initialize();
  148.     for i in 0..board.len() {
  149.         for j in 0..board[0].len() {
  150.             if board[i][j] == '#' {
  151.                 let new_key: Vec<i32> = vec![i as i32, j as i32, 0, 0];
  152.                 grid.set(&new_key, '#');
  153.             }
  154.         }
  155.     }
  156.     return grid;
  157. }
  158. fn update_board(grid: &mut Grid) {
  159.     let voxels_to_fill = grid.get_voxels_to_fill();
  160.     grid.active_to_keep();
  161.     for voxel_to_fill in voxels_to_fill {
  162.         grid.set(&voxel_to_fill, '#');
  163.     }
  164. }
  165.  
  166. fn get_board(file_name: &String) -> Vec<Vec<char>> {
  167.     let file = File::open(file_name)
  168.         .expect("Could not open file");
  169.     let buf_reader = BufReader::new(file);
  170.     let mut data: Vec<Vec<char>> = Vec::new();
  171.     for line in buf_reader.lines() {
  172.         let mut row: Vec<char> = Vec::new();
  173.         if !line.is_ok() {
  174.             continue;
  175.         }
  176.         for c in line.unwrap().chars() {
  177.             row.push(c);
  178.         }
  179.         data.push(row);
  180.     }
  181.     return data;
  182. }
  183.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×