-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay17.java
67 lines (55 loc) · 2.1 KB
/
Day17.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.sbaars.adventofcode.year15.days;
import com.sbaars.adventofcode.year15.Day2015;
import java.util.*;
import java.util.stream.Collectors;
public class Day17 extends Day2015 {
private static final int TARGET_LITERS = 150;
private final List<Integer> containers = new ArrayList<>();
private final Map<Integer, Integer> containerCountMap = new HashMap<>();
public Day17() {
super(17);
parseInput();
}
public static void main(String[] args) {
Day17 day = new Day17();
day.printParts();
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 17, 1);
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 17, 2);
}
private void parseInput() {
Arrays.stream(day().split("\n"))
.map(String::trim)
.filter(s -> !s.isEmpty())
.map(Integer::parseInt)
.forEach(containers::add);
}
@Override
public Object part1() {
return findCombinations(0, 0, new boolean[containers.size()], 0);
}
@Override
public Object part2() {
containerCountMap.clear();
findCombinations(0, 0, new boolean[containers.size()], 0);
int minContainers = containerCountMap.keySet().stream()
.min(Integer::compareTo)
.orElse(0);
return containerCountMap.get(minContainers);
}
private int findCombinations(int index, int currentSum, boolean[] used, int containerCount) {
if (currentSum == TARGET_LITERS) {
containerCountMap.merge(containerCount, 1, Integer::sum);
return 1;
}
if (currentSum > TARGET_LITERS || index >= containers.size()) {
return 0;
}
// Don't use current container
int withoutCurrent = findCombinations(index + 1, currentSum, used, containerCount);
// Use current container
used[index] = true;
int withCurrent = findCombinations(index + 1, currentSum + containers.get(index), used, containerCount + 1);
used[index] = false;
return withoutCurrent + withCurrent;
}
}