From 9a8b9f97543c03a0f515722cc9fdd17c49b1771e Mon Sep 17 00:00:00 2001 From: Flashk Date: Fri, 15 Dec 2023 07:39:25 +0100 Subject: [PATCH] Minor adjustments to day 15 --- .../flashk/day15/LensLibrary.java | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/adventofcode/flashk/day15/LensLibrary.java b/src/main/java/com/adventofcode/flashk/day15/LensLibrary.java index becdb91..28e58b2 100644 --- a/src/main/java/com/adventofcode/flashk/day15/LensLibrary.java +++ b/src/main/java/com/adventofcode/flashk/day15/LensLibrary.java @@ -1,6 +1,7 @@ package com.adventofcode.flashk.day15; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; @@ -12,13 +13,11 @@ public class LensLibrary { private static final Pattern STEP_PATTERN = Pattern.compile("([a-z]*)([=-])([1-9]*)"); - private String[] steps; - private int stepsNumber; - private List> boxes = new ArrayList<>(); + private final String[] steps; + private final List> boxes = new ArrayList<>(); public LensLibrary(String input) { steps = input.split(","); - stepsNumber = steps.length; for(int i = 0; i < 256; i++) { boxes.add(new LinkedHashMap<>()); @@ -26,12 +25,8 @@ public LensLibrary(String input) { } - public long solveA() { - long currentValue = 0; - for(String step : steps) { - currentValue += hash(step); - } - return currentValue; + public int solveA() { + return Arrays.stream(steps).map(this::hash).reduce(0, Integer::sum); } public long solveB() { @@ -65,7 +60,7 @@ private void addLens(String label, int focalLength) { private long calculateBoxes() { long finalResult = 0; - for(int boxIndex = 0; boxIndex focalLengths = boxes.get(boxIndex).values(); int slot = 1; for(int focalLength : focalLengths) { @@ -81,10 +76,7 @@ private int hash(String label) { int currentValue = 0; char[] wordCharacters = label.toCharArray(); for(char character : wordCharacters) { - currentValue += (int) character; - if(currentValue == 10) { - System.out.println("New character on word = '"+label+"'- Character: "+character); - } + currentValue += character; currentValue *= 17; currentValue %= 256; }