Skip to content

Commit c321def

Browse files
committed
Solved day 17 part 2
1 parent 4738cb0 commit c321def

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-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.16.3'
7+
version '0.16.4'
88

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

day17/src/main/java/de/havox_design/aoc2015/day17/NotEnoughEggnod.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ public static int solvePart2(String fileName) {
2929
return instance.solvePart2();
3030
}
3131

32+
protected static int countLimitedCombinations(String fileName, int amountOfEggnod) {
33+
NotEnoughEggnod instance = new NotEnoughEggnod(fileName);
34+
int minContainerRequirement = instance.findMinimumContainerCount(instance.input, 0, amountOfEggnod, 0);
35+
return instance.countWaysWithLimitedContainers(instance.input, 0, amountOfEggnod, 0, minContainerRequirement);
36+
}
37+
3238
public int solvePart1() {
3339
return countWays(input, 0, 150, 0);
3440
}
3541

3642
public int solvePart2() {
37-
return 0;
43+
int minContainerRequirement = findMinimumContainerCount(input, 0, 150, 0);
44+
return countWaysWithLimitedContainers(input, 0, 150, 0, minContainerRequirement);
3845
}
3946

4047
private int countWays(List<Integer> containers, int startIndex, int target, int countSoFar) {
@@ -51,6 +58,36 @@ private int countWays(List<Integer> containers, int startIndex, int target, int
5158
}
5259
}
5360

61+
private int findMinimumContainerCount(List<Integer> containers, int startIndex, int target, int containerCount) {
62+
if (target == 0) {
63+
return containerCount;
64+
} else if (target < 0) {
65+
return Integer.MAX_VALUE;
66+
} else {
67+
int min = Integer.MAX_VALUE;
68+
for (int i = startIndex; i < containers.size(); ++i) {
69+
min = Math.min(min,
70+
findMinimumContainerCount(containers, i + 1, target - containers.get(i), containerCount + 1));
71+
}
72+
return min;
73+
}
74+
}
75+
76+
private int countWaysWithLimitedContainers(List<Integer> containers, int startIndex, int target, int countSoFar, int usableContainers) {
77+
if (target == 0 && usableContainers >= 0) {
78+
return 1;
79+
} else if (target < 0 || usableContainers < 0) {
80+
return 0;
81+
} else {
82+
int count = countSoFar;
83+
for (int i = startIndex; i < containers.size(); ++i) {
84+
count += countWaysWithLimitedContainers(containers, i + 1, target - containers.get(i), countSoFar,
85+
usableContainers - 1);
86+
}
87+
return count;
88+
}
89+
}
90+
5491
private List<String> readData(String fileName) {
5592
return DataReader.readData(fileName, MainClass.class);
5693
}

day17/src/test/java/de/havox_design/aoc2015/day17/Day17Test.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,19 @@ void testPart1(String fileName, int expectedNumberOfCombinations) {
2424

2525
private static Stream<Arguments> getDataForPart1() {
2626
return Stream.of(
27-
Arguments.of("part1sample.txt", 4)
27+
Arguments.of("sample.txt", 4)
28+
);
29+
}
30+
31+
@ParameterizedTest
32+
@MethodSource("getDataForPart2")
33+
void testPart2(String fileName, int expectedNumberOfCombinations) {
34+
Assertions.assertEquals(expectedNumberOfCombinations, NotEnoughEggnod.countLimitedCombinations(fileName, 25));
35+
}
36+
37+
private static Stream<Arguments> getDataForPart2() {
38+
return Stream.of(
39+
Arguments.of("sample.txt", 3)
2840
);
2941
}
3042
}

0 commit comments

Comments
 (0)