Skip to content

Commit fb6fe6f

Browse files
committed
2015 Day 17
1 parent e11a470 commit fb6fe6f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.anne.aoc2015;
2+
3+
import org.anne.common.Day;
4+
5+
import java.util.List;
6+
7+
public class Day17 extends Day {
8+
public static void main(String[] args) {
9+
new Day17().run();
10+
}
11+
12+
@Override
13+
public void execute() {
14+
setName("No Such Thing as Too Much");
15+
List<Integer> input = readFileAsInts();
16+
setPart1(part1(input, 150));
17+
setPart2(part2(input, 150));
18+
}
19+
20+
public static int part1(List<Integer> input, int target) {
21+
return findNbCombinations(input, target, false);
22+
}
23+
24+
public static int part2(List<Integer> input, int target) {
25+
return findNbCombinations(input, target, true);
26+
}
27+
28+
private static int findNbCombinations(List<Integer> input, int target, boolean usingMinContainers) {
29+
int minContainers = Integer.MAX_VALUE;
30+
int count = 0;
31+
32+
for (int i = 0; i < (1 << input.size()); i++) {
33+
int sum = 0;
34+
int numContainers = 0;
35+
36+
for (int j = 0; j < input.size(); j++) {
37+
if ((i & (1 << j)) > 0) {
38+
sum += input.get(j);
39+
numContainers++;
40+
}
41+
}
42+
43+
if (sum == target) {
44+
if (usingMinContainers && numContainers < minContainers) {
45+
minContainers = numContainers;
46+
count = 1;
47+
} else if (!usingMinContainers || numContainers == minContainers) {
48+
count++;
49+
}
50+
}
51+
}
52+
53+
return count;
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.anne.aoc2015;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
class Day17Test {
11+
12+
List<Integer> input = List.of(20, 15, 10, 5, 5);
13+
14+
@Test
15+
void testPart1() {
16+
assertEquals(4, Day17.part1(input, 25));
17+
}
18+
19+
@Test
20+
void testPart2() {
21+
assertEquals(3, Day17.part2(input, 25));
22+
}
23+
}

0 commit comments

Comments
 (0)