38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
|
use crate::prelude::*;
|
||
|
|
||
|
mod grid;
|
||
|
|
||
|
use grid::*;
|
||
|
|
||
|
pub fn solution() -> Solution {
|
||
|
Solution::new(Day::new(11, 2020), part_1, part_2).with_expected(2243, 2027)
|
||
|
}
|
||
|
|
||
|
fn calculate(input: &str, ruleset: Ruleset) -> usize {
|
||
|
let mut grid = Grid::new(input, ruleset);
|
||
|
|
||
|
let edge_cell_count = grid.count_cells(Cell::Edge);
|
||
|
let floor_cell_count = grid.count_cells(Cell::Floor);
|
||
|
|
||
|
let mut previous_occupied_cell_count = None;
|
||
|
while previous_occupied_cell_count != Some(grid.occupied_cell_count) {
|
||
|
previous_occupied_cell_count = Some(grid.occupied_cell_count);
|
||
|
grid.cells = grid.simulate_step();
|
||
|
grid.occupied_cell_count = grid.count_cells(Cell::Occupied);
|
||
|
|
||
|
// Sanity check, make sure no edge or floor cells are ever changed.
|
||
|
assert_eq!(edge_cell_count, grid.count_cells(Cell::Edge));
|
||
|
assert_eq!(floor_cell_count, grid.count_cells(Cell::Floor));
|
||
|
}
|
||
|
|
||
|
previous_occupied_cell_count.unwrap()
|
||
|
}
|
||
|
|
||
|
fn part_1(input: &str) -> Result<String> {
|
||
|
Ok(calculate(input, Ruleset::PartOne).to_string())
|
||
|
}
|
||
|
|
||
|
fn part_2(input: &str) -> Result<String> {
|
||
|
Ok(calculate(input, Ruleset::PartTwo).to_string())
|
||
|
}
|