Skip to content

Commit 6e2cfe2

Browse files
committed
Solved day 24 part 1
1 parent 8c90395 commit 6e2cfe2

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
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.1'
7+
version '0.23.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.havox_design.aoc2015.day24;
2+
3+
import java.util.*;
4+
5+
public class LengthSortedResult implements Iterable<Integer[]> {
6+
7+
private final List<Integer[]>[] data;
8+
9+
private int size;
10+
11+
public LengthSortedResult(int maxlength) {
12+
data = new List[maxlength];
13+
for (int i = 0; i < data.length; i++) {
14+
data[i] = new ArrayList<>();
15+
}
16+
}
17+
18+
public void add(Integer[] pair) {
19+
data[pair.length].add(pair);
20+
size++;
21+
}
22+
23+
@Override
24+
public Iterator<Integer[]> iterator() {
25+
return Arrays.stream(data).flatMap(Collection::stream).iterator();
26+
}
27+
28+
public int size() {
29+
return size;
30+
}
31+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class MainClass {
66
private static final Logger LOGGER = Logger.getLogger(MainClass.class.getName());
77

88
public static void main(String[] args) {
9-
LOGGER.info("Solution 1: 13");
10-
LOGGER.info("Solution 2: 23");
9+
LOGGER.info(() -> "Solution 1: " + BalancedQuantumEntanglement.solvePart1("day24.txt"));
10+
LOGGER.info(() -> "Solution 2: " + BalancedQuantumEntanglement.solvePart2("day24.txt"));
1111
}
1212
}

0 commit comments

Comments
 (0)