|
| 1 | +package de.havox_design.aoc2015.day24; |
| 2 | + |
| 3 | +import de.havox_design.aoc2015.utils.DataReader; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +public class BalancedQuantumEntanglement { |
| 10 | + private final List<String> input; |
| 11 | + |
| 12 | + public BalancedQuantumEntanglement(String fileName) { |
| 13 | + input = readData(fileName); |
| 14 | + } |
| 15 | + |
| 16 | + public static long solvePart1(String fileName) { |
| 17 | + BalancedQuantumEntanglement instance = new BalancedQuantumEntanglement(fileName); |
| 18 | + return instance.solvePart1(); |
| 19 | + } |
| 20 | + |
| 21 | + public static long solvePart2(String fileName) { |
| 22 | + BalancedQuantumEntanglement instance = new BalancedQuantumEntanglement(fileName); |
| 23 | + return instance.solvePart2(); |
| 24 | + } |
| 25 | + |
| 26 | + public long solvePart1() { |
| 27 | + List<Integer> weights = input |
| 28 | + .stream() |
| 29 | + .map(Integer::valueOf) |
| 30 | + .toList(); |
| 31 | + int weight = weights |
| 32 | + .stream() |
| 33 | + .mapToInt(Integer::intValue) |
| 34 | + .sum(); |
| 35 | + |
| 36 | + return findSolution(weights, weight / 3); |
| 37 | + } |
| 38 | + |
| 39 | + public long solvePart2() { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + private long findSolution(final List<Integer> weights, final int groupWeight) { |
| 44 | + final LengthSortedResult combinations = calcCombinations(weights, groupWeight); |
| 45 | + for (Integer[] firstCombination : combinations) { |
| 46 | + List<Integer> remaining = difference(weights, firstCombination); |
| 47 | + final LengthSortedResult combinationsForSecondGroup = calcCombinations(remaining, groupWeight); |
| 48 | + if (combinationsForSecondGroup.size() > 0) { |
| 49 | + for (final Integer[] secondCombination : combinationsForSecondGroup) { |
| 50 | + final LengthSortedResult combinationsForThirdGroup = calcCombinations(difference(remaining, secondCombination), groupWeight); |
| 51 | + if (combinationsForThirdGroup.size() > 0) { |
| 52 | + return quantumEntanglement(firstCombination); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return -1; |
| 58 | + } |
| 59 | + private LengthSortedResult calcCombinations(List<Integer> weights, int groupWeight) { |
| 60 | + LengthSortedResult res = new LengthSortedResult(weights.size() + 1); |
| 61 | + calcCombinations(weights, groupWeight, new ArrayList<>(), 0, res); |
| 62 | + return res; |
| 63 | + } |
| 64 | + |
| 65 | + private void calcCombinations( |
| 66 | + final List<Integer> weights, |
| 67 | + final int remaining, |
| 68 | + final List<Integer> combination, |
| 69 | + final int startAt, |
| 70 | + final LengthSortedResult result |
| 71 | + ) { |
| 72 | + for (int i = startAt; i < weights.size(); i++) { |
| 73 | + final Integer current = weights.get(i); |
| 74 | + if (current > remaining) { |
| 75 | + break; |
| 76 | + } |
| 77 | + combination.add(current); |
| 78 | + if (remaining == current) { |
| 79 | + result.add(combination.toArray(Integer[]::new)); |
| 80 | + } else { |
| 81 | + calcCombinations(weights, remaining - current, combination, i + 1, result); |
| 82 | + } |
| 83 | + combination.remove(combination.size() - 1); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private List<Integer> difference(final List<Integer> elements, final Integer[] elementsToRemove) { |
| 88 | + final List<Integer> remaining = new ArrayList<>(elements); |
| 89 | + for (final Integer elementToRemove : elementsToRemove) { |
| 90 | + remaining.remove(elementToRemove); |
| 91 | + } |
| 92 | + return remaining; |
| 93 | + } |
| 94 | + |
| 95 | + public final long quantumEntanglement(final Integer[] list) { |
| 96 | + long result = list[0]; |
| 97 | + for (int i = list.length - 1; i > 0; i--) { |
| 98 | + result *= list[i]; |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + private List<String> readData(String fileName) { |
| 104 | + return DataReader.readData(fileName, MainClass.class); |
| 105 | + } |
| 106 | +} |
0 commit comments