Skip to content

Commit d678f21

Browse files
committed
Solved day 24 part 2
1 parent 6206ffb commit d678f21

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.23.3'
7+
version '0.23.4'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day24/src/main/java/de/havox_design/aoc2015/day24/BalancedQuantumEntanglement.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.util.ArrayList;
66
import java.util.List;
7-
import java.util.stream.Collectors;
87

98
public 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

Comments
 (0)