Skip to content
Snippets Groups Projects
Select Git revision
  • 574093f8fadfa2f87114014832eb66f149b08c50
  • main default protected
2 results

Puzzle.java

Blame
  • Puzzle.java 1.52 KiB
    package edu.unl.cse.bohn;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public abstract class Puzzle {
        protected String sampleData = "";
        protected boolean isProductionReady;
    
        public Puzzle(boolean isProductionReady) {
            this.isProductionReady = isProductionReady;
        }
    
        public abstract long computePart1(List<String> data);
        public abstract long computePart2(List<String> data);
    
        public void solvePuzzle(ImportData dataSource) {
            List<String> data = null;
            try {
                data = isProductionReady ? dataSource.importData() : List.of(sampleData.split(System.lineSeparator()));
            } catch (IOException ioException) {
                System.err.println("Could not retrieve data: " + ioException);
                System.exit(1);
            }
            System.out.println("Part 1: " + computePart1(data));
            System.out.println("Part 2: " + computePart2(data));
        }
    
        protected static List<Integer> toIntegerList(List<String> data) {
            return data.stream().mapToInt(Integer::valueOf).boxed().toList();
        }
    
        protected static char[][] toCharMatrix(List<String> data) {
            // I suspect this cast won't work
            return (char[][]) data.stream().map(String::toCharArray).toArray();
        }
    
        protected static int[][] toIntMatrix(List<String> data) {
            int[][] matrix = new int[data.size()][data.get(0).length()];
            Arrays.setAll(matrix, i -> data.get(i).chars().map(c -> c - '0').toArray());
            return matrix;
        }
    }