Created
January 5, 2020 16:28
-
-
Save svantelidman/1e8c291350f0466e3e3665cbbe06f399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This was swiftly accomplished but part_2 will be quite different... | |
*/ | |
use std::io::{BufReader, BufRead}; | |
use std::fs::File; | |
use std::collections::HashSet; | |
type Habitat = Vec<Vec<char>>; | |
fn main() { | |
let mut layout_history: HashSet<Habitat> = HashSet::new(); | |
let habitat_file = "habitat".to_string(); | |
let mut habitat = load_habitat(&habitat_file); | |
layout_history.insert(habitat.clone()); | |
loop { | |
habitat = tick_habitat_clock(&habitat); | |
if layout_history.contains(&habitat) { | |
println!("Biodiversity for first reappearing layout={}", biodiversity_rating(&habitat)); | |
break; | |
} | |
layout_history.insert(habitat.clone()); | |
} | |
} | |
fn load_habitat(habitat_file: &String) -> Habitat { | |
let mut habitat = Habitat::new(); | |
let file = File::open(habitat_file).expect("Could not open habitat file!"); | |
let reader = BufReader::new(file); | |
for line in reader.lines() { | |
let line = line.expect("No string there."); | |
let chrs: Vec<char> = line.as_str().trim().chars().collect(); | |
if chrs.len() != 5 { | |
panic!("Malformed habitat file: {}", habitat_file) | |
} | |
habitat.push(chrs) | |
} | |
if habitat.len() != 5 { | |
panic!("Malformed habitat file: {}", habitat_file) | |
} | |
habitat | |
} | |
fn transform_cell(row: i64, col: i64, habitat: &Habitat) -> char { | |
let cell_offsets = [(1, 0), (-1, 0), (0, 1), (0, -1)]; | |
let mut n_bugs = 0; | |
for offset in &cell_offsets { | |
let n_row = row + offset.0; | |
let n_col = col + offset.1; | |
if n_row >= 0 && n_row <= 4 && n_col >= 0 && n_col <= 4 && habitat[n_row as usize][n_col as usize] == '#' { | |
n_bugs += 1; | |
} | |
} | |
if habitat[row as usize][col as usize] == '#' && n_bugs != 1 { | |
'.' | |
} else if habitat[row as usize][col as usize] == '.' && (n_bugs == 1 || n_bugs == 2) { | |
'#' | |
} else { | |
habitat[row as usize][col as usize] | |
} | |
} | |
fn tick_habitat_clock(habitat: &Habitat) -> Habitat { | |
let mut transformed_habitat = habitat.clone(); | |
for row in 0..5 { | |
for col in 0..5 { | |
transformed_habitat[row][col] = transform_cell(row as i64, col as i64, habitat) | |
} | |
} | |
transformed_habitat | |
} | |
fn biodiversity_rating(habitat: &Habitat) -> i64 { | |
let mut acc_pt = 0; | |
let mut cell_pt = 1; | |
for row in habitat { | |
for c in row { | |
if *c == '#' { | |
acc_pt += cell_pt | |
} | |
cell_pt += cell_pt | |
} | |
} | |
acc_pt | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
#[test] | |
fn test_1() { | |
let start_file = "test1_start".to_string(); | |
let start_habitat = load_habitat(&start_file); | |
let min4_file = "test1_4min".to_string(); | |
let min4_habitat = load_habitat(&min4_file); | |
let mut habitat = start_habitat.clone(); | |
for _ in 0..4 { | |
habitat = tick_habitat_clock(&habitat) | |
} | |
assert_eq!(min4_habitat, habitat) | |
} | |
#[test] | |
fn test_biodiversity() { | |
let habitat_file = "test_biodiversity".to_string(); | |
let habitat = load_habitat(&habitat_file); | |
assert_eq!(2129920, biodiversity_rating(&habitat)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment