Skip to content

Commit

Permalink
Minor adjustments to day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 15, 2023
1 parent 66a680a commit 9a8b9f9
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/main/java/com/adventofcode/flashk/day15/LensLibrary.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,26 +13,20 @@ public class LensLibrary {

private static final Pattern STEP_PATTERN = Pattern.compile("([a-z]*)([=-])([1-9]*)");

private String[] steps;
private int stepsNumber;
private List<Map<String,Integer>> boxes = new ArrayList<>();
private final String[] steps;
private final List<Map<String,Integer>> boxes = new ArrayList<>();

public LensLibrary(String input) {
steps = input.split(",");
stepsNumber = steps.length;

for(int i = 0; i < 256; i++) {
boxes.add(new LinkedHashMap<>());
}

}

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() {
Expand Down Expand Up @@ -65,7 +60,7 @@ private void addLens(String label, int focalLength) {
private long calculateBoxes() {
long finalResult = 0;

for(int boxIndex = 0; boxIndex <boxes.size(); boxIndex++) {
for(int boxIndex = 0; boxIndex < boxes.size(); boxIndex++) {
Collection<Integer> focalLengths = boxes.get(boxIndex).values();
int slot = 1;
for(int focalLength : focalLengths) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 9a8b9f9

Please sign in to comment.