Class Grid<T>

java.lang.Object
com.expedient.adventofcodejade.common.Grid<T>

public class Grid<T> extends Object
Manages a 2D array and provides methods to interact with it
  • Method Details

    • fromStringList

      public static Grid<Character> fromStringList(List<String> lines)
    • findTrailStepUnique

      public static int findTrailStepUnique(Grid<Character> grid, Coordinate center, int currentStep)
      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

      public boolean isSafe(Coordinate location)
      Returns false if the given coordinate would be out of bounds
      Parameters:
      location - a coordinate
      Returns:
      false if oob
    • at

      public T at(int row, int col)
      Get the value at the given coordinates
      Parameters:
      row - value row
      col - value column
      Returns:
      the value of type T
    • at

      public T at(Coordinate c)
      Get the value at the given Coordinate
      Parameters:
      c - coordinate for value
      Returns:
      the value of type T
    • set

      public void set(int row, int col, T val)
      Set the value at the given coordinates
      Parameters:
      row - value row
      col - value column
      val - the value to set
    • set

      public void set(Coordinate c, T val)
      Set the value at the given Coordinate
      Parameters:
      c - value Coordinate
      val - the value to set
    • getArray

      public T[][] getArray()
      Returns the backing array for the Grid
      Returns:
      2D backing array
    • setArray

      public void setArray(T[][] newArray)
      Sets the backing array for the Grid
      Parameters:
      newArray - 2D array
    • cloneArray

      public T[][] cloneArray()
      Returns a deep copy of the backing array for the Grid
      Returns:
      deep copy of the 2D backing array
    • checkCorner

      public int checkCorner(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 many
      Parameters:
      center - the given coordinate
      test - a test that determines whether a coordinate is part of the area
      Returns:
      the number of "corners" the given coordinate represents
    • safeNeighborCoordinates

      public List<Coordinate> safeNeighborCoordinates(Coordinate center, boolean orthogonalOnly)
      Returns a list of Coordinates that surround the value at row, col in a given 2d array, respecting bounds
      Parameters:
      center - Coordinate of center
      orthogonalOnly - whether to exclude diagonals from output
      Returns:
      a list of in-bounds Coordinates that surround the center point
    • matchCoordinates

      public List<Coordinate> matchCoordinates(Predicate<T> test)
      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

      public List<Coordinate> matchCoordinatesByCoordinate(Predicate<Coordinate> test)
      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

      public List<Coordinate> coordinatesWithinTaxicabDistance(Coordinate one, int maxDistance)
    • adjacentCoordinates

      public List<Coordinate> adjacentCoordinates(Coordinate center, boolean vertical)
      Returns coordinates to the left and right or up and down from the given center point, respecting bounds
      Parameters:
      center - Coordinate of center
      vertical - whether to give up and down rather than left and right
      Returns:
      list of Coordinates adjacent to the center point
    • checkNeighbors

      public boolean checkNeighbors(Coordinate center, Predicate<T> test, boolean orthogonalOnly)
      Checks if any values surrounding a given point in a 2d array pass the provided test
      Parameters:
      center - Coordinate of center
      test - the test run against the values surrounding the center point
      orthogonalOnly - whether to exclude diagonals from the test
      Returns:
      whether any surrounding points pass the test
    • checkNeighbors

      public boolean checkNeighbors(Coordinate center, Predicate<T> test)
      Checks if any values surrounding a given point in a 2d array pass the provided test
      Parameters:
      center - Coordinate of center
      test - 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 center
      test - the test run against the values surrounding the center point
      orthogonalOnly - whether to exclude diagonals from the test
      Returns:
      list of Coordinates of surrounding values that pass the test
    • matchNeighbors

      public List<Coordinate> matchNeighbors(Coordinate center, Predicate<T> test)
      Gets a list of Coordinates surrounding a given point in a 2d array that pass the provided test
      Parameters:
      center - Coordinate of center
      test - the test run against the values surrounding the center point
      Returns:
      list of Coordinates of surrounding values that pass the test
    • floodFill

      public Grid<T> floodFill(Coordinate center, Predicate<T> test, Function<T,T> transformation)
      Performs a flood fill operation on the provided 2D array, using the given test and transformation
      Parameters:
      center - Coordinate of center
      test - 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 perimeter
      test - a test to determine whether a coordinate is part of the region
      prev - 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 count
      test - a test to determine whether a coordinate is part of the region
      prev - a set of coordinates that already have been checked. may be null.
      Returns:
      the number of sides of the contiguous region
    • findContiguousRegionArea

      public Integer findContiguousRegionArea(Coordinate center, Predicate<T> test, Set<Coordinate> prev)
      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 area
      test - a test to determine whether a coordinate is part of the region
      prev - a set of coordinates that already have been checked. may be null.
      Returns:
      the area of the contiguous region
    • print

      public void print(Function<Coordinate,Character> characterFunction)
      Prints the 2D array to stdout
    • print

      public void print()
      Prints the 2D array to stdout
    • duplicate

      public Grid<T> duplicate()
      Performs a deep copy of the backing array and then returns a new Grid instance from the copy
      Returns:
      Fresh, cloned Grid