Struct advent_of_code_2021::day_25::Grid [−][src]
struct Grid {
cells: Vec<Cell>,
width: usize,
height: usize,
active_right: HashSet<(usize, usize)>,
active_down: HashSet<(usize, usize)>,
}Expand description
Represent a grid as a vector of cells, with a width and height to enable quick lookups from x/y co-ordinates, and
to help with wrapping around logic. Also keep HashSets of the RIGHT and DOWN cells that may be able to move,
to limit the cells we need to check when iterating the grid
Fields
cells: Vec<Cell>The cells of the grid as a single list
width: usizeCache the grid width
height: usizeCache teh grid height
active_right: HashSet<(usize, usize)>The cells with a RIGHTwards moving sea cucumber that may be able to move
active_down: HashSet<(usize, usize)>The cells with a DOWNwards moving sea cucumber that may be able to move
Implementations
Get the current value of a given cell co-ordinate, or None if it is out of bounds for the grid
Convert x, y co-ordinates to an index in the underlying list of cells.
Swap the values of two cells - used when sea cucumbers move
We are overly optimistic when building the active sets. Given the co-ordinates of a sea cucumber and it’s direction of travel, check if it’s next cell is actually available to move into.
Do a full iteration of the grid in-place, moving RIGHTs that can move rightwards, then DOWNs that can move downwards. Calculate the new active set as all the cucumbers that moved, plus any that can move into the vacated space.
