Skip to content

Commit de686cb

Browse files
committed
Solved Day 17 (2015) - No Such Thing as Too Much
1 parent 1039738 commit de686cb

File tree

1 file changed

+53
-17
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+53
-17
lines changed
Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,59 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
46

57
public class Day17 extends Day2015 {
6-
public Day17() {
7-
super(17);
8-
}
9-
10-
public static void main(String[] args) {
11-
new Day17().printParts();
12-
}
13-
14-
@Override
15-
public Object part1() {
16-
return "";
17-
}
18-
19-
@Override
20-
public Object part2() {
21-
return "";
22-
}
8+
9+
private static final int TARGET_LITERS = 150;
10+
private final List<Integer> containers = new ArrayList<>();
11+
12+
public Day17() {
13+
super(17);
14+
parseInput();
15+
}
16+
17+
public static void main(String[] args) {
18+
Day17 day = new Day17();
19+
day.printParts();
20+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 17, 1);
21+
}
22+
23+
private void parseInput() {
24+
Arrays.stream(day().split("\n"))
25+
.map(String::trim)
26+
.filter(s -> !s.isEmpty())
27+
.map(Integer::parseInt)
28+
.forEach(containers::add);
29+
}
30+
31+
@Override
32+
public Object part1() {
33+
return findCombinations(0, 0, new boolean[containers.size()]);
34+
}
35+
36+
@Override
37+
public Object part2() {
38+
return 0; // Implement in next part
39+
}
40+
41+
private int findCombinations(int index, int currentSum, boolean[] used) {
42+
if (currentSum == TARGET_LITERS) {
43+
return 1;
44+
}
45+
if (currentSum > TARGET_LITERS || index >= containers.size()) {
46+
return 0;
47+
}
48+
49+
// Don't use current container
50+
int withoutCurrent = findCombinations(index + 1, currentSum, used);
51+
52+
// Use current container
53+
used[index] = true;
54+
int withCurrent = findCombinations(index + 1, currentSum + containers.get(index), used);
55+
used[index] = false;
56+
57+
return withoutCurrent + withCurrent;
58+
}
2359
}

0 commit comments

Comments
 (0)