|
1 | 1 | package com.sbaars.adventofcode.year15.days;
|
2 | 2 |
|
3 | 3 | import com.sbaars.adventofcode.year15.Day2015;
|
| 4 | +import java.util.*; |
| 5 | +import java.util.stream.Collectors; |
4 | 6 |
|
5 | 7 | 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 | + } |
23 | 59 | }
|
0 commit comments