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 87efef4

Browse files
Solve day 17 2015 part 2: No Such Thing as Too Much
1 parent ca23b87 commit 87efef4

File tree

1 file changed

+13
-5
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+13
-5
lines changed

‎src/main/java/com/sbaars/adventofcode/year15/days/Day17.java‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class Day17 extends Day2015 {
88

99
private static final int TARGET_LITERS = 150;
1010
private final List<Integer> containers = new ArrayList<>();
11+
private final Map<Integer, Integer> containerCountMap = new HashMap<>();
1112

1213
public Day17() {
1314
super(17);
@@ -18,6 +19,7 @@ public static void main(String[] args) {
1819
Day17 day = new Day17();
1920
day.printParts();
2021
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 17, 1);
22+
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 17, 2);
2123
}
2224

2325
private void parseInput() {
@@ -30,28 +32,34 @@ private void parseInput() {
3032

3133
@Override
3234
public Object part1() {
33-
return findCombinations(0, 0, new boolean[containers.size()]);
35+
return findCombinations(0, 0, new boolean[containers.size()], 0);
3436
}
3537

3638
@Override
3739
public Object part2() {
38-
return 0; // Implement in next part
40+
containerCountMap.clear();
41+
findCombinations(0, 0, new boolean[containers.size()], 0);
42+
int minContainers = containerCountMap.keySet().stream()
43+
.min(Integer::compareTo)
44+
.orElse(0);
45+
return containerCountMap.get(minContainers);
3946
}
4047

41-
private int findCombinations(int index, int currentSum, boolean[] used) {
48+
private int findCombinations(int index, int currentSum, boolean[] used, intcontainerCount) {
4249
if (currentSum == TARGET_LITERS) {
50+
containerCountMap.merge(containerCount, 1, Integer::sum);
4351
return 1;
4452
}
4553
if (currentSum > TARGET_LITERS || index >= containers.size()) {
4654
return 0;
4755
}
4856

4957
// Don't use current container
50-
int withoutCurrent = findCombinations(index + 1, currentSum, used);
58+
int withoutCurrent = findCombinations(index + 1, currentSum, used, containerCount);
5159

5260
// Use current container
5361
used[index] = true;
54-
int withCurrent = findCombinations(index + 1, currentSum + containers.get(index), used);
62+
int withCurrent = findCombinations(index + 1, currentSum + containers.get(index), used, containerCount + 1);
5563
used[index] = false;
5664

5765
return withoutCurrent + withCurrent;

0 commit comments

Comments
(0)

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