11package com .adventofcode .flashk .day07 ;
22
33import lombok .Getter ;
4+ import org .apache .commons .lang3 .StringUtils ;
45
56import java .util .Arrays ;
67import java .util .Deque ;
@@ -14,42 +15,35 @@ public class Equation {
1415 private boolean concatenate ;
1516
1617 public Equation (String input ) {
17- String [] inputParts = input .split (":" );
18+ String [] inputParts = input .split (": " );
1819 result = Long .parseLong (inputParts [0 ]);
19- operators = Arrays .stream (inputParts [1 ].split (" " ) )
20- .skip ( 1 ). map ( Integer :: parseInt ). collect (Collectors .toCollection (ArrayDeque ::new ));
20+ operators = Arrays .stream (inputParts [1 ].split (StringUtils . SPACE )). map ( Integer :: valueOf )
21+ .collect (Collectors .toCollection (ArrayDeque ::new ));
2122 }
2223
2324 public long solve (boolean concatenate ) {
2425 this .concatenate = concatenate ;
25- return hasSolution (operators , 0 ) ? result : 0 ;
26+ return hasSolution (0 ) ? result : 0 ;
2627 }
2728
28- private boolean hasSolution (Deque < Integer > operators , long partialResult ) {
29+ private boolean hasSolution (long partialResult ) {
2930
3031 if (operators .isEmpty ()) {
3132 return partialResult == result ;
3233 }
3334
3435 Integer currentOperator = operators .poll ();
35- boolean hasSolution = hasSolution (operators , partialResult + currentOperator );
36+ boolean hasSolution = hasSolution (partialResult + currentOperator );
3637
3738 if (!hasSolution && partialResult != 0 ) {
38- hasSolution = hasSolution (operators , partialResult * currentOperator );
39+ hasSolution = hasSolution (partialResult * currentOperator );
3940 } else if (partialResult == 0 ) {
40- hasSolution = hasSolution (operators , currentOperator );
41+ hasSolution = hasSolution (currentOperator );
4142 }
4243
4344 if (!hasSolution && concatenate ) {
44- StringBuilder sb = new StringBuilder ();
45-
46- if (partialResult != 0 ) {
47- sb .append (partialResult );
48- }
49-
50- long concatenatedPartialResult = Long .parseLong (sb .append (currentOperator ).toString ());
51- hasSolution = hasSolution (operators , concatenatedPartialResult );
52-
45+ long concatenatedPartialResult = Long .parseLong (String .valueOf (partialResult ) + currentOperator );
46+ hasSolution = hasSolution (concatenatedPartialResult );
5347 }
5448
5549 operators .addFirst (currentOperator );
0 commit comments