Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b7d419

Browse files
Solve day 24 2015 part 1: It Hangs in the Balance
1 parent 77769f8 commit 4b7d419

File tree

1 file changed

+84
-3
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+84
-3
lines changed
Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,105 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
46

57
public class Day24 extends Day2015 {
8+
private final List<Long> packages;
9+
private final long totalWeight;
610

711
public Day24() {
812
super(24);
13+
packages = Arrays.stream(day().split("\n"))
14+
.mapToLong(Long::parseLong)
15+
.boxed()
16+
.collect(Collectors.toList());
17+
totalWeight = packages.stream().mapToLong(Long::longValue).sum();
918
}
1019

1120
public static void main(String[] args) {
12-
new Day24().printParts();
21+
Day24 day = new Day24();
22+
day.printParts();
23+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 24, 1);
1324
}
1425

1526
@Override
1627
public Object part1() {
17-
return "";
28+
return findOptimalConfiguration(3);
1829
}
1930

2031
@Override
2132
public Object part2() {
22-
return "";
33+
return findOptimalConfiguration(4);
34+
}
35+
36+
private long findOptimalConfiguration(int numGroups) {
37+
long targetWeight = totalWeight / numGroups;
38+
int minSize = Integer.MAX_VALUE;
39+
long minQE = Long.MAX_VALUE;
40+
41+
// Try all possible combinations for group 1
42+
for (int size = 1; size <= packages.size(); size++) {
43+
if (size >= minSize) break; // No need to check larger groups
44+
45+
List<List<Long>> combinations = findCombinations(packages, size, targetWeight);
46+
if (!combinations.isEmpty()) {
47+
// Found valid combinations with this size
48+
minSize = size;
49+
// Find minimum quantum entanglement among these combinations
50+
for (List<Long> combo : combinations) {
51+
if (canSplitRemaining(packages, combo, targetWeight, numGroups - 1)) {
52+
long qe = calculateQE(combo);
53+
minQE = Math.min(minQE, qe);
54+
}
55+
}
56+
if (minQE != Long.MAX_VALUE) break; // Found valid configuration
57+
}
58+
}
59+
return minQE;
60+
}
61+
62+
private List<List<Long>> findCombinations(List<Long> nums, int size, long target) {
63+
List<List<Long>> result = new ArrayList<>();
64+
findCombinationsHelper(nums, size, target, 0, new ArrayList<>(), result);
65+
return result;
66+
}
67+
68+
private void findCombinationsHelper(List<Long> nums, int size, long target, int start,
69+
List<Long> current, List<List<Long>> result) {
70+
if (current.size() == size) {
71+
if (current.stream().mapToLong(Long::longValue).sum() == target) {
72+
result.add(new ArrayList<>(current));
73+
}
74+
return;
75+
}
76+
77+
for (int i = start; i < nums.size(); i++) {
78+
current.add(nums.get(i));
79+
findCombinationsHelper(nums, size, target, i + 1, current, result);
80+
current.remove(current.size() - 1);
81+
}
82+
}
83+
84+
private boolean canSplitRemaining(List<Long> allNums, List<Long> used, long targetWeight, int groups) {
85+
if (groups == 1) return true;
86+
87+
List<Long> remaining = new ArrayList<>(allNums);
88+
remaining.removeAll(used);
89+
90+
// Try different sizes for the next group
91+
for (int size = 1; size <= remaining.size(); size++) {
92+
List<List<Long>> combinations = findCombinations(remaining, size, targetWeight);
93+
for (List<Long> combo : combinations) {
94+
if (canSplitRemaining(remaining, combo, targetWeight, groups - 1)) {
95+
return true;
96+
}
97+
}
98+
}
99+
return false;
100+
}
101+
102+
private long calculateQE(List<Long> nums) {
103+
return nums.stream().reduce(1L, (a, b) -> a * b);
23104
}
24105
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /