Struct advent_of_code_2021::util::grid::Grid [−][src]
Expand description
A representation of a 2D grid of u8s. Originally implemented for crate::day_9, another grid was needed for
crate::day_11 and so common methods were extracted to this shared module
Fields
numbers: Vec<u8>Store the numbers in a 1D list…
width: usize…and use the width to determine the 1D offset as a 2D co-ordinate
Implementations
Iterate through the four orthogonal cells, collecting the 2 - 4 values into a vector. Include the co-ordinates
in the returned vector so that Grid::get_basin can recursively expand the set of cells in the basin.
This is the core logic to implement a single pass of the octopuses powering up required for both parts. It first iterates through all the cells incrementing them by one. Any that started at 9 are added to a queue of cells that have triggered a flash. We then take cells off the to flash list, adding them to a set of all co-ordinates that have flashed this iteration (so that we can 0 them later, and also ensure no cell flashes twice). If the cell was new to the flashes set, it also increments its neighbours, adding any that exceed 9 to the trigger queue. Once the queue has been exhausted, we iterate through the resulting set of co-ordinates that flashed this iteration, set them to 0 and return the size of the set, as this is the metric needed for both parts’ solutions.
Solution to part one. Iterate the grid
Solution to part two. Iterate the grid until the set of flashes is the same size as the grid, i.e. all cells triggered a flash. Return the number of iterations required to reach that point.
Return a list of the co-ordinates and values of all local minima
The risk level of the grid is the sum of the risk level of each low point, which is the low point’s height plus one.
Recursively walk to higher points from a starting minimum, stopping at the watershed of height 9. Returns the set of co-ordinates found.
pub fn iter(&self) -> GridCoords<'_>ⓘ
pub fn iter(&self) -> GridCoords<'_>ⓘ
Helper to abstract iterating over the whole grid
Turn (y, x) coordinates into a position in the underlying array
The co-ordinates of the bottom right corner in (y, x) format
Used by GridCoords::next and other iterators over the grid , e.g. Grid::iterate_and_flash to turn the
current iterator position into the x/y co-ordinates and the value in that cell.
Iterate through the four orthogonal cells, collecting the 2 - 4 values into a vector. Include the co-ordinates
in the returned vector so that Grid::get_basin can recursively expand the set of cells in the basin.
Given a cell and a delta, return the new co-ordinates and the value at those co-ordinates if it is within the grid, None otherwise.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Grid
impl UnwindSafe for Grid
Blanket Implementations
Mutably borrows from an owned value. Read more
