Class Grid<T>
java.lang.Object
com.expedient.adventofcodejade.common.Grid<T>
-
Method Summary
Modifier and TypeMethodDescriptionadjacentCoordinates(Coordinate center, boolean vertical) Returns coordinates to the left and right or up and down from the given center point, respecting boundsat(int row, int col) Get the value at the given coordinatesat(Coordinate c) Get the value at the given CoordinateintcheckCorner(Coordinate center, Predicate<T> test) Given a coordinate and a test, determines whether the given coordinate represents a "corner" of a contiguous area of coordinates that pass the test, and if so, how manybooleancheckNeighbors(Coordinate center, Predicate<T> test) Checks if any values surrounding a given point in a 2d array pass the provided testbooleancheckNeighbors(Coordinate center, Predicate<T> test, boolean orthogonalOnly) Checks if any values surrounding a given point in a 2d array pass the provided testT[][]Returns a deep copy of the backing array for the GridintcolCount()Get the column countcoordinatesWithinTaxicabDistance(Coordinate one, int maxDistance) coordinatesWithinTaxicabDistance(Coordinate one, int maxDistance, Predicate<T> test) Performs a deep copy of the backing array and then returns a new Grid instance from the copyfindContiguousRegionArea(Coordinate center, Predicate<T> test, Set<Coordinate> prev) Finds the number of coordinates in a contiguous area, defined by whether coordinates pass the given testfindContiguousRegionPerimeter(Coordinate center, Predicate<T> test, Set<Coordinate> prev) Finds the perimeter of the contiguous region that includes the given coordinate center.findContiguousRegionSides(Coordinate center, Predicate<T> test, Set<Coordinate> prev) Finds the number of sides of the contiguous region that includes the given coordinate center.static Set<Coordinate> findTrailStepScore(Grid<Character> grid, Coordinate center, int currentStep) static intfindTrailStepUnique(Grid<Character> grid, Coordinate center, int currentStep) Performs a flood fill operation on the provided 2D array, using the given test and transformationfromStringList(List<String> lines) T[][]getArray()Returns the backing array for the GridbooleanisSafe(Coordinate location) Returns false if the given coordinate would be out of boundsmatchCoordinates(Predicate<T> test) Returns a list of Coordinates at which lie objects of type T that match the conditions of the given testReturns a list of Coordinates from the Grid that match the conditions of the given testmatchNeighbors(Coordinate center, Predicate<T> test) Gets a list of Coordinates surrounding a given point in a 2d array that pass the provided testmatchNeighbors(Coordinate center, Predicate<T> test, boolean orthogonalOnly) Gets a list of Coordinates surrounding a given point in a 2d array that pass the provided testvoidprint()Prints the 2D array to stdoutvoidprint(Function<Coordinate, Character> characterFunction) Prints the 2D array to stdoutintrowCount()Get the row countsafeNeighborCoordinates(Coordinate center, boolean orthogonalOnly) Returns a list of Coordinates that surround the value at row, col in a given 2d array, respecting boundsvoidSet the value at the given coordinatesvoidset(Coordinate c, T val) Set the value at the given CoordinatevoidSets the backing array for the Grid
-
Method Details
-
fromStringList
-
findTrailStepUnique
- Parameters:
grid-center-currentStep-- Returns:
-
findTrailStepScore
public static Set<Coordinate> findTrailStepScore(Grid<Character> grid, Coordinate center, int currentStep) -
rowCount
public int rowCount()Get the row count- Returns:
- the Grid's row count
-
colCount
public int colCount()Get the column count- Returns:
- the Grid's column count
-
isSafe
Returns false if the given coordinate would be out of bounds- Parameters:
location- a coordinate- Returns:
- false if oob
-
at
Get the value at the given coordinates- Parameters:
row- value rowcol- value column- Returns:
- the value of type T
-
at
Get the value at the given Coordinate- Parameters:
c- coordinate for value- Returns:
- the value of type T
-
set
Set the value at the given coordinates- Parameters:
row- value rowcol- value columnval- the value to set
-
set
Set the value at the given Coordinate- Parameters:
c- value Coordinateval- the value to set
-
getArray
-
setArray
Sets the backing array for the Grid- Parameters:
newArray- 2D array
-
cloneArray
Returns a deep copy of the backing array for the Grid- Returns:
- deep copy of the 2D backing array
-
checkCorner
Given a coordinate and a test, determines whether the given coordinate represents a "corner" of a contiguous area of coordinates that pass the test, and if so, how many- Parameters:
center- the given coordinatetest- a test that determines whether a coordinate is part of the area- Returns:
- the number of "corners" the given coordinate represents
-
safeNeighborCoordinates
Returns a list of Coordinates that surround the value at row, col in a given 2d array, respecting bounds- Parameters:
center- Coordinate of centerorthogonalOnly- whether to exclude diagonals from output- Returns:
- a list of in-bounds Coordinates that surround the center point
-
matchCoordinates
Returns a list of Coordinates at which lie objects of type T that match the conditions of the given test- Parameters:
test- test that can be performed on a coordinate- Returns:
- a List of coordinates at which lie objects that match the conditions
-
matchCoordinatesByCoordinate
Returns a list of Coordinates from the Grid that match the conditions of the given test- Parameters:
test- test that can be performed on a Coordinate- Returns:
- a List of Coordinates satisfying the test conditions
-
coordinatesWithinTaxicabDistance
public List<Coordinate> coordinatesWithinTaxicabDistance(Coordinate one, int maxDistance, Predicate<T> test) -
coordinatesWithinTaxicabDistance
-
adjacentCoordinates
Returns coordinates to the left and right or up and down from the given center point, respecting bounds- Parameters:
center- Coordinate of centervertical- whether to give up and down rather than left and right- Returns:
- list of Coordinates adjacent to the center point
-
checkNeighbors
Checks if any values surrounding a given point in a 2d array pass the provided test- Parameters:
center- Coordinate of centertest- the test run against the values surrounding the center pointorthogonalOnly- whether to exclude diagonals from the test- Returns:
- whether any surrounding points pass the test
-
checkNeighbors
Checks if any values surrounding a given point in a 2d array pass the provided test- Parameters:
center- Coordinate of centertest- the test run against the values surrounding the center point- Returns:
- whether any surrounding points pass the test
-
matchNeighbors
public List<Coordinate> matchNeighbors(Coordinate center, Predicate<T> test, boolean orthogonalOnly) Gets a list of Coordinates surrounding a given point in a 2d array that pass the provided test- Parameters:
center- Coordinate of centertest- the test run against the values surrounding the center pointorthogonalOnly- whether to exclude diagonals from the test- Returns:
- list of Coordinates of surrounding values that pass the test
-
matchNeighbors
Gets a list of Coordinates surrounding a given point in a 2d array that pass the provided test- Parameters:
center- Coordinate of centertest- the test run against the values surrounding the center point- Returns:
- list of Coordinates of surrounding values that pass the test
-
floodFill
Performs a flood fill operation on the provided 2D array, using the given test and transformation- Parameters:
center- Coordinate of centertest- the test run to determine whether the value is "inside"transformation- the operation to apply to the values. this should also invalidate the "inside" test
-
findContiguousRegionPerimeter
public Integer findContiguousRegionPerimeter(Coordinate center, Predicate<T> test, Set<Coordinate> prev) Finds the perimeter of the contiguous region that includes the given coordinate center.- Parameters:
center- the coordinate from which to calculate the contiguous region's perimetertest- a test to determine whether a coordinate is part of the regionprev- a set of coordinates that already have been checked. may be null.- Returns:
- the perimeter of the contiguous region
-
findContiguousRegionSides
public Integer findContiguousRegionSides(Coordinate center, Predicate<T> test, Set<Coordinate> prev) Finds the number of sides of the contiguous region that includes the given coordinate center. The calculation takes advantage of the fact that any given polygon has the same number of sides as it has corners, so we don't have to worry about maintaining state or awareness of sides, and can instead check neighboring coordinates to see if each coordinate is a corner.- Parameters:
center- the coordinate from which to calculate the contiguous region's side counttest- a test to determine whether a coordinate is part of the regionprev- a set of coordinates that already have been checked. may be null.- Returns:
- the number of sides of the contiguous region
-
findContiguousRegionArea
Finds the number of coordinates in a contiguous area, defined by whether coordinates pass the given test- Parameters:
center- the coordinate from which to calculate the contiguous region's areatest- a test to determine whether a coordinate is part of the regionprev- a set of coordinates that already have been checked. may be null.- Returns:
- the area of the contiguous region
-
print
Prints the 2D array to stdout -
print
public void print()Prints the 2D array to stdout -
duplicate
-