Struct advent_of_code_2021::util::grid::Grid[−][src]

pub struct Grid {
    pub numbers: Vec<u8>,
    pub width: usize,
}
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 times, summing the flashes this causes.

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.

Is the provided grid cell a local minimum

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.

Iterate through the local minima, find the basin size of each, and return the highest three sizes found

Helper to abstract iterating over the whole grid

Return the value at the given co-ordinates

Update the value in a given cell

Turn (y, x) coordinates into a position in the underlying array

The co-ordinates of the bottom right corner in (y, x) format

The number of cells in the grid

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.

Dump the grid to stdout - useful for visualising the grid when debugging

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Build an untiled wrapper from a given sub-grid. See also ExpandedGrid::with_copies

Turn the characters into digits and concatenate, caching the width

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.