Made my own Ref type

This commit is contained in:
Sugui 2023-12-03 15:04:58 +01:00
parent 6e80331dd3
commit db6e65bd53
3 changed files with 20 additions and 20 deletions

View File

@ -14,14 +14,14 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import sugui.util.IntegerRef; import sugui.util.Ref;
import sugui.util.Pair; import sugui.util.Pair;
public class Day03 { public class Day03 {
public static Path inputPath = Paths.get("resources/input03.txt"); public static Path inputPath = Paths.get("resources/input03.txt");
private static Map<Pair<Integer, Integer>, IntegerRef> numbers = new HashMap<>(); private static Map<Pair<Integer, Integer>, Ref<Integer>> numbers = new HashMap<>();
private static Map<Pair<Integer, Integer>, String> symbols = new HashMap<>(); private static Map<Pair<Integer, Integer>, String> symbols = new HashMap<>();
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
@ -42,7 +42,7 @@ public class Day03 {
Matcher numberMatcher = numberPattern.matcher(line); Matcher numberMatcher = numberPattern.matcher(line);
Matcher symbolMatcher = symbolPattern.matcher(line); Matcher symbolMatcher = symbolPattern.matcher(line);
while (numberMatcher.find()) { while (numberMatcher.find()) {
IntegerRef numberRef = new IntegerRef(Integer.parseInt(numberMatcher.group())); Ref<Integer> numberRef = new Ref<Integer>(Integer.parseInt(numberMatcher.group()));
for (int col = numberMatcher.start(); col < numberMatcher.end(); col++) { for (int col = numberMatcher.start(); col < numberMatcher.end(); col++) {
numbers.put(new Pair<>(col, lineNumber), numberRef); numbers.put(new Pair<>(col, lineNumber), numberRef);
} }
@ -59,13 +59,13 @@ public class Day03 {
loadNumbersAndSymbols(input); loadNumbersAndSymbols(input);
int sumOfPartNumbers = 0; int sumOfPartNumbers = 0;
Set<IntegerRef> visitedNumbers = new HashSet<>(); Set<Ref<Integer>> visitedNumbers = new HashSet<>();
for (var symbolPositions : symbols.keySet()) { for (var symbolPositions : symbols.keySet()) {
for (int dy = -1; dy <= 1; dy++) { for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) { for (int dx = -1; dx <= 1; dx++) {
var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy); var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy);
if (numbers.containsKey(position)) { if (numbers.containsKey(position)) {
IntegerRef numberRef = numbers.get(position); Ref<Integer> numberRef = numbers.get(position);
if (!visitedNumbers.contains(numberRef)) { if (!visitedNumbers.contains(numberRef)) {
visitedNumbers.add(numberRef); visitedNumbers.add(numberRef);
sumOfPartNumbers += numberRef.value(); sumOfPartNumbers += numberRef.value();
@ -83,12 +83,12 @@ public class Day03 {
int sumOfGearRatios = 0; int sumOfGearRatios = 0;
for (var symbolPositions : symbols.keySet()) { for (var symbolPositions : symbols.keySet()) {
if ("*".equals(symbols.get(symbolPositions))) { if ("*".equals(symbols.get(symbolPositions))) {
Set<IntegerRef> visitedNumbers = new HashSet<>(); Set<Ref<Integer>> visitedNumbers = new HashSet<>();
for (int dy = -1; dy <= 1; dy++) { for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) { for (int dx = -1; dx <= 1; dx++) {
var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy); var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy);
if (numbers.containsKey(position)) { if (numbers.containsKey(position)) {
IntegerRef numberRef = numbers.get(position); Ref<Integer> numberRef = numbers.get(position);
if (!visitedNumbers.contains(numberRef)) { if (!visitedNumbers.contains(numberRef)) {
visitedNumbers.add(numberRef); visitedNumbers.add(numberRef);
} }

View File

@ -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;
}
}

View File

@ -0,0 +1,13 @@
package sugui.util;
public class Ref<T> {
private T value;
public Ref(T value) {
this.value = value;
}
public T value() {
return value;
}
}