44
55import java .util .ArrayList ;
66import java .util .List ;
7- import java .util .stream .Collectors ;
87
98public class BalancedQuantumEntanglement {
109 private final List <String > input ;
@@ -24,6 +23,14 @@ public static long solvePart2(String fileName) {
2423 }
2524
2625 public long solvePart1 () {
26+ return processDay24 (3 );
27+ }
28+
29+ public long solvePart2 () {
30+ return processDay24 (4 );
31+ }
32+
33+ private long processDay24 (int numberOfGroups ) {
2734 List <Integer > weights = input
2835 .stream ()
2936 .map (Integer ::valueOf )
@@ -33,19 +40,19 @@ public long solvePart1() {
3340 .mapToInt (Integer ::intValue )
3441 .sum ();
3542
36- return findSolution (weights , weight / 3 );
43+ return findSolution (weights , weight / numberOfGroups , numberOfGroups == 4 );
3744 }
3845
39- public long solvePart2 () {
40- return 0 ;
41- }
42-
43- private long findSolution (final List <Integer > weights , final int groupWeight ) {
46+ private long findSolution (final List <Integer > weights , final int groupWeight , final boolean isPart2 ) {
4447 final LengthSortedResult combinations = calcCombinations (weights , groupWeight );
4548 for (Integer [] firstCombination : combinations ) {
4649 List <Integer > remaining = difference (weights , firstCombination );
4750 final LengthSortedResult combinationsForSecondGroup = calcCombinations (remaining , groupWeight );
48- if (combinationsForSecondGroup .size () > 0 ) {
51+ if (isPart2 ) {
52+ if (combinationsForSecondGroup .size () > 0 ) {
53+ return quantumEntanglement (firstCombination );
54+ }
55+ } else if (combinationsForSecondGroup .size () > 0 ) {
4956 for (final Integer [] secondCombination : combinationsForSecondGroup ) {
5057 final LengthSortedResult combinationsForThirdGroup = calcCombinations (difference (remaining , secondCombination ), groupWeight );
5158 if (combinationsForThirdGroup .size () > 0 ) {
@@ -77,7 +84,7 @@ private void calcCombinations(
7784 combination .add (current );
7885 if (remaining == current ) {
7986 result .add (combination .toArray (Integer []::new ));
80- } else {
87+ } else if ( remaining > current ) {
8188 calcCombinations (weights , remaining - current , combination , i + 1 , result );
8289 }
8390 combination .remove (combination .size () - 1 );
0 commit comments