@@ -8,6 +8,7 @@ public class Day17 extends Day2015 {
8
8
9
9
private static final int TARGET_LITERS = 150 ;
10
10
private final List <Integer > containers = new ArrayList <>();
11
+ private final Map <Integer , Integer > containerCountMap = new HashMap <>();
11
12
12
13
public Day17 () {
13
14
super (17 );
@@ -18,6 +19,7 @@ public static void main(String[] args) {
18
19
Day17 day = new Day17 ();
19
20
day .printParts ();
20
21
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 );
21
23
}
22
24
23
25
private void parseInput () {
@@ -30,28 +32,34 @@ private void parseInput() {
30
32
31
33
@ Override
32
34
public Object part1 () {
33
- return findCombinations (0 , 0 , new boolean [containers .size ()]);
35
+ return findCombinations (0 , 0 , new boolean [containers .size ()], 0 );
34
36
}
35
37
36
38
@ Override
37
39
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 );
39
46
}
40
47
41
- private int findCombinations (int index , int currentSum , boolean [] used ) {
48
+ private int findCombinations (int index , int currentSum , boolean [] used , int containerCount ) {
42
49
if (currentSum == TARGET_LITERS ) {
50
+ containerCountMap .merge (containerCount , 1 , Integer ::sum );
43
51
return 1 ;
44
52
}
45
53
if (currentSum > TARGET_LITERS || index >= containers .size ()) {
46
54
return 0 ;
47
55
}
48
56
49
57
// Don't use current container
50
- int withoutCurrent = findCombinations (index + 1 , currentSum , used );
58
+ int withoutCurrent = findCombinations (index + 1 , currentSum , used , containerCount );
51
59
52
60
// Use current container
53
61
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 );
55
63
used [index ] = false ;
56
64
57
65
return withoutCurrent + withCurrent ;
0 commit comments