|
1 | 1 | package com.sbaars.adventofcode.year15.days;
|
2 | 2 |
|
3 | 3 | import com.sbaars.adventofcode.year15.Day2015;
|
| 4 | +import java.util.*; |
| 5 | +import java.util.stream.Collectors; |
4 | 6 |
|
5 | 7 | public class Day24 extends Day2015 {
|
| 8 | + private final List<Long> packages; |
| 9 | + private final long totalWeight; |
6 | 10 |
|
7 | 11 | public Day24() {
|
8 | 12 | 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(); |
9 | 18 | }
|
10 | 19 |
|
11 | 20 | 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); |
13 | 24 | }
|
14 | 25 |
|
15 | 26 | @Override
|
16 | 27 | public Object part1() {
|
17 | | - return ""; |
| 28 | + return findOptimalConfiguration(3); |
18 | 29 | }
|
19 | 30 |
|
20 | 31 | @Override
|
21 | 32 | 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); |
23 | 104 | }
|
24 | 105 | }
|
0 commit comments