Skip to content

Commit

Permalink
refactor: improve day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 7, 2024
1 parent e294201 commit 0cb6c2b
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/main/java/com/adventofcode/flashk/day07/Equation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adventofcode.flashk.day07;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Deque;
Expand All @@ -14,42 +15,35 @@ public class Equation {
private boolean concatenate;

public Equation(String input) {
String[] inputParts = input.split(":");
String[] inputParts = input.split(": ");
result = Long.parseLong(inputParts[0]);
operators = Arrays.stream(inputParts[1].split(" "))
.skip(1).map(Integer::parseInt).collect(Collectors.toCollection(ArrayDeque::new));
operators = Arrays.stream(inputParts[1].split(StringUtils.SPACE)).map(Integer::valueOf)
.collect(Collectors.toCollection(ArrayDeque::new));
}

public long solve(boolean concatenate) {
this.concatenate = concatenate;
return hasSolution(operators, 0) ? result : 0;
return hasSolution(0) ? result : 0;
}

private boolean hasSolution(Deque<Integer> operators, long partialResult) {
private boolean hasSolution(long partialResult) {

if(operators.isEmpty()) {
return partialResult == result;
}

Integer currentOperator = operators.poll();
boolean hasSolution = hasSolution(operators, partialResult + currentOperator);
boolean hasSolution = hasSolution(partialResult + currentOperator);

if(!hasSolution && partialResult != 0) {
hasSolution = hasSolution(operators, partialResult * currentOperator);
hasSolution = hasSolution(partialResult * currentOperator);
} else if(partialResult == 0) {
hasSolution = hasSolution(operators, currentOperator);
hasSolution = hasSolution(currentOperator);
}

if(!hasSolution && concatenate) {
StringBuilder sb = new StringBuilder();

if(partialResult != 0) {
sb.append(partialResult);
}

long concatenatedPartialResult = Long.parseLong(sb.append(currentOperator).toString());
hasSolution = hasSolution(operators, concatenatedPartialResult);

long concatenatedPartialResult = Long.parseLong(String.valueOf(partialResult) + currentOperator);
hasSolution = hasSolution(concatenatedPartialResult);
}

operators.addFirst(currentOperator);
Expand Down

0 comments on commit 0cb6c2b

Please sign in to comment.