1
Fork 0
advent-of-code/source/year_2020/day_11/mod.rs

44 lines
1.3 KiB
Rust

//! Day 11 of 2020.
use crate::prelude::*;
mod grid;
use grid::*;
/// Get the solution for day 11 of 2020.
pub fn solution() -> Solution {
Solution::new(Day::new(11, 2020), part_1, part_2).with_expected(2243, 2027)
}
/// Calculate the result based on the ruleset.
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()
}
/// The logic to solve part one.
fn part_1(input: &str) -> Result<String> {
Ok(calculate(input, Ruleset::PartOne).to_string())
}
/// The logic to solve part two.
fn part_2(input: &str) -> Result<String> {
Ok(calculate(input, Ruleset::PartTwo).to_string())
}