From 574093f8fadfa2f87114014832eb66f149b08c50 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Fri, 9 Dec 2022 08:02:58 -0600 Subject: [PATCH] Added helper methods to convert data into common forms --- 2022/README.md | 18 ++++++++++++++++++ .../java/edu/unl/cse/bohn/year2022/Day8.java | 6 +----- scaffolding/Puzzle.java | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/2022/README.md b/2022/README.md index 6092a83..b0c0ae5 100644 --- a/2022/README.md +++ b/2022/README.md @@ -252,5 +252,23 @@ Well... I'm going to take the part 2 code in a (very) slightly different directi ## Day 9 +- [The problem](https://adventofcode.com/2022/day/9) +- [The solution](src/main/java/edu/unl/cse/bohn/year2022/Day9.java) + +### Part 1 + +The subproblems are +- ... + +### Part 2 + +... + +### Refactoring + +... + +## Day 10 + (coming soon) diff --git a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day8.java b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day8.java index 35daf52..da581f8 100644 --- a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day8.java +++ b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day8.java @@ -39,11 +39,7 @@ public class Day8 extends Puzzle { } private static int[][] getTreeHeights(List<String> data) { - int[][] treeHeights = new int[data.size()][data.get(0).length()]; - for (int i = 0; i < treeHeights.length; i++) { - treeHeights[i] = data.get(i).chars().map(c -> c - '0').toArray(); - } - return treeHeights; + return toIntMatrix(data); } private boolean[][] computeTreeVisibility(List<String> data) { diff --git a/scaffolding/Puzzle.java b/scaffolding/Puzzle.java index 100704c..f4ca424 100644 --- a/scaffolding/Puzzle.java +++ b/scaffolding/Puzzle.java @@ -1,6 +1,8 @@ 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 { @@ -25,4 +27,19 @@ public abstract class Puzzle { 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; + } } -- GitLab