Class PuzzleInput

java.lang.Object
com.expedient.adventofcodejade.common.PuzzleInput

public class PuzzleInput extends Object
Handles both sample and actual input files
  • Method Details

    • fromPath

      public static PuzzleInput fromPath(String inputPath) throws IOException
      Constructs a PuzzleInput from a given filesystem path
      Parameters:
      inputPath - path where the input is located
      Returns:
      PuzzleInput object that contains the file's contents
      Throws:
      IOException - when the file is not found
    • fromResource

      public static PuzzleInput fromResource(String resourcePath) throws IOException
      Constructs a PuzzleInput from the given resource path
      Parameters:
      resourcePath - path of resource (contained in /src/main/resources)
      Returns:
      PuzzleInput object that contains the resource's contents
      Throws:
      IOException - when the given resource can't be loaded
    • sampleForDay

      public static PuzzleInput sampleForDay(int year, int day, boolean partOne) throws IOException
      Gets a PuzzleInput using the resource for a specific day (located in /src/main/resources). Will first attempt to find sample input with the naming scheme [day]-[part] in case sample differs between parts 1 and 2, otherwise falls back to input with the naming scheme [day]
      Parameters:
      day - day for which the input is loaded
      partOne - whether the sample is being fetched for part one
      Returns:
      PuzzleInput object containing the given day's sample input
      Throws:
      IOException - when the given day's input cannot be found in resources
    • isTest

      public boolean isTest()
    • getLines

      public List<String> getLines()
      Get the lines from the file
      Returns:
      a list of strings containing the input as-is
    • getString

      public String getString()
      Get the input as a single string, rather than as a list of lines
      Returns:
      String containing the entire input
    • getTwoLists

      public <T> List<List<T>> getTwoLists(Function<String,T> conversion, String delimiterRegex)
      Return two lists based on split lines from the input
      Type Parameters:
      T - the type contained within the returned lists
      Parameters:
      conversion - function used to convert the string into a type of your choice
      delimiterRegex - delimiter used to split the lines
      Returns:
      two lists, composed of the elements that were split from each input
    • allMatchesPerLine

      public <T> List<List<T>> allMatchesPerLine(Predicate<Character> test, Function<Character,T> conversion, Function<String,String> transform)
      Converts each line to a list of type T values, after iterating over each Character in the line and applying a provided filter function. Optionally can apply a transformation to the line string before running the filter.
      Type Parameters:
      T - The desired output type for the operation
      Parameters:
      test - Test to determine whether a given value will be included in the list for each line
      conversion - Function to convert the Character value to type T
      transform - Function to apply to each line before processing it. If this is null, the transformation step is skipped
      Returns:
      List of lists composed of values from each line matching the test function and converted to type T
    • getGrid

      public Grid<Character> getGrid() throws IllegalArgumentException
      Returns the input as a rectangular 2D array of Character. Can't use generics because generic array creation is not possible. The input must not be empty and must be composed of equally sized rows.
      Returns:
      2D array of Character derived from input
      Throws:
      IllegalArgumentException - if the array would be ragged / the rows are of differing lengths
    • day5Input

      public Pair<List<Pair<Integer,Integer>>,List<List<Integer>>> day5Input()
      Converts all starting lines with numbers separated by '|' characters into Pairs of Integers, then creates a List of Lists containing Integers based on the comma separated lists of numbers in the second half of the input, separated from the first section by a blank line
      Returns:
      A Pair containing the List of Pairs of Integers, as well as the List of Lists of Integers