Module advent_of_code_2021::day_11 [−][src]
Expand description
This is my solution for Advent of Code - Day 11 - Dumbo Octopus
Today was again using a grid of digits. This time we had to manage cascading updates as one cell overflowing beyond 9 caused neighbouring cells to increase, potentially triggering more cells. If you’ve ever played Pandemic, today’s puzzle may give you flashbacks.
The grid shared a lot of similarities with the grid in crate::day_9, and so I reused that implementation
adding extra methods to meet today’s needs. Whilst writing the solution I just imported the Grid directly from
crate::day_9, but once submitted I did some cleanup refactoring and extracted it into its own module under
crate::util, leaving the puzzle specific methods in their own impl blocks in the relevant module. Having a
comprehensive set of tests made this a pretty easy refactor.
Grid::set was needed to allow incrementing the values in the cells as the octopuses powered up.
Grid::get_all_surrounds was needed separate to Grid::get_orthogonal_surrounds as today’s
requirements considered diagonal cells adjacent. This caused some head scratching when I failed to notice this
difference, but Grid::print let me visualise the grid and work out what was wrong.
The bulk of today’s solutions is handled by Grid::iterate_and_flash which handles a single cycle of
incrementing the octopuses, and resolving any resulting flashes. Grid::count_flashes implements part one by
repeatedly calling Grid::iterate_and_flash the required number of times, summing the resulting flash counts.
Grid::run_until_sync also repeatedly calls Grid::iterate_and_flash until the count of flashes is equal to
the number of cells in the grid, indicating all octopuses flashed in sync, and returns the iteration it has reached.
Re-exports
pub use crate::util::grid::Grid;Functions
The entry point for running the solutions with the ‘real’ puzzle input.
