From db6e65bd5397744b8676ce36b815387bfe1ed04a Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 3 Dec 2023 15:04:58 +0100 Subject: [PATCH] Made my own Ref type --- src/main/java/sugui/Day03.java | 14 +++++++------- src/main/java/sugui/util/IntegerRef.java | 13 ------------- src/main/java/sugui/util/Ref.java | 13 +++++++++++++ 3 files changed, 20 insertions(+), 20 deletions(-) delete mode 100644 src/main/java/sugui/util/IntegerRef.java create mode 100644 src/main/java/sugui/util/Ref.java diff --git a/src/main/java/sugui/Day03.java b/src/main/java/sugui/Day03.java index 079014a..95f2da3 100644 --- a/src/main/java/sugui/Day03.java +++ b/src/main/java/sugui/Day03.java @@ -14,14 +14,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import sugui.util.IntegerRef; +import sugui.util.Ref; import sugui.util.Pair; public class Day03 { public static Path inputPath = Paths.get("resources/input03.txt"); - private static Map, IntegerRef> numbers = new HashMap<>(); + private static Map, Ref> numbers = new HashMap<>(); private static Map, String> symbols = new HashMap<>(); public static void main(String[] args) throws IOException { @@ -42,7 +42,7 @@ public class Day03 { Matcher numberMatcher = numberPattern.matcher(line); Matcher symbolMatcher = symbolPattern.matcher(line); while (numberMatcher.find()) { - IntegerRef numberRef = new IntegerRef(Integer.parseInt(numberMatcher.group())); + Ref numberRef = new Ref(Integer.parseInt(numberMatcher.group())); for (int col = numberMatcher.start(); col < numberMatcher.end(); col++) { numbers.put(new Pair<>(col, lineNumber), numberRef); } @@ -59,13 +59,13 @@ public class Day03 { loadNumbersAndSymbols(input); int sumOfPartNumbers = 0; - Set visitedNumbers = new HashSet<>(); + Set> visitedNumbers = new HashSet<>(); for (var symbolPositions : symbols.keySet()) { for (int dy = -1; dy <= 1; dy++) { for (int dx = -1; dx <= 1; dx++) { var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy); if (numbers.containsKey(position)) { - IntegerRef numberRef = numbers.get(position); + Ref numberRef = numbers.get(position); if (!visitedNumbers.contains(numberRef)) { visitedNumbers.add(numberRef); sumOfPartNumbers += numberRef.value(); @@ -83,12 +83,12 @@ public class Day03 { int sumOfGearRatios = 0; for (var symbolPositions : symbols.keySet()) { if ("*".equals(symbols.get(symbolPositions))) { - Set visitedNumbers = new HashSet<>(); + Set> visitedNumbers = new HashSet<>(); for (int dy = -1; dy <= 1; dy++) { for (int dx = -1; dx <= 1; dx++) { var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy); if (numbers.containsKey(position)) { - IntegerRef numberRef = numbers.get(position); + Ref numberRef = numbers.get(position); if (!visitedNumbers.contains(numberRef)) { visitedNumbers.add(numberRef); } diff --git a/src/main/java/sugui/util/IntegerRef.java b/src/main/java/sugui/util/IntegerRef.java deleted file mode 100644 index 650460e..0000000 --- a/src/main/java/sugui/util/IntegerRef.java +++ /dev/null @@ -1,13 +0,0 @@ -package sugui.util; - -public class IntegerRef { - private int value; - - public IntegerRef(int value) { - this.value = value; - } - - public int value() { - return value; - } -} diff --git a/src/main/java/sugui/util/Ref.java b/src/main/java/sugui/util/Ref.java new file mode 100644 index 0000000..963b57b --- /dev/null +++ b/src/main/java/sugui/util/Ref.java @@ -0,0 +1,13 @@ +package sugui.util; + +public class Ref { + private T value; + + public Ref(T value) { + this.value = value; + } + + public T value() { + return value; + } +}