@@ -20,6 +20,7 @@ public static void main(String[] args) {
20
20
Day15 day = new Day15 ();
21
21
day .printParts ();
22
22
new com .sbaars .adventofcode .network .Submit ().submit (day .part1 (), 2015 , 15 , 1 );
23
+ new com .sbaars .adventofcode .network .Submit ().submit (day .part2 (), 2015 , 15 , 2 );
23
24
}
24
25
25
26
private void parseInput () {
@@ -45,7 +46,7 @@ public Object part1() {
45
46
46
47
@ Override
47
48
public Object part2 () {
48
- return 0 ; // Implement in next part
49
+ return findBestScoreWithCalories ( 500 );
49
50
}
50
51
51
52
private long findBestScore () {
@@ -56,6 +57,15 @@ private long findBestScore() {
56
57
.orElse (0 );
57
58
}
58
59
60
+ private long findBestScoreWithCalories (int targetCalories ) {
61
+ return generateCombinations (TOTAL_TEASPOONS , ingredients .size ())
62
+ .stream ()
63
+ .filter (amounts -> calculateCalories (amounts ) == targetCalories )
64
+ .mapToLong (this ::calculateScore )
65
+ .max ()
66
+ .orElse (0 );
67
+ }
68
+
59
69
private List <List <Integer >> generateCombinations (int total , int parts ) {
60
70
List <List <Integer >> result = new ArrayList <>();
61
71
generateCombinationsHelper (total , parts , new ArrayList <>(), result );
@@ -100,6 +110,14 @@ private long calculateScore(List<Integer> amounts) {
100
110
return (long ) capacity * durability * flavor * texture ;
101
111
}
102
112
113
+ private int calculateCalories (List <Integer > amounts ) {
114
+ int calories = 0 ;
115
+ for (int i = 0 ; i < ingredients .size (); i ++) {
116
+ calories += ingredients .get (i ).calories * amounts .get (i );
117
+ }
118
+ return calories ;
119
+ }
120
+
103
121
private static class Ingredient {
104
122
private final String name ;
105
123
private final int capacity ;
0 commit comments