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