Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1009afe

Browse files
Solve day 15 2015 part 2: Science for Hungry People
1 parent ab939c8 commit 1009afe

File tree

1 file changed

+19
-1
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+19
-1
lines changed

‎src/main/java/com/sbaars/adventofcode/year15/days/Day15.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static void main(String[] args) {
2020
Day15 day = new Day15();
2121
day.printParts();
2222
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);
2324
}
2425

2526
private void parseInput() {
@@ -45,7 +46,7 @@ public Object part1() {
4546

4647
@Override
4748
public Object part2() {
48-
return 0; // Implement in next part
49+
return findBestScoreWithCalories(500);
4950
}
5051

5152
private long findBestScore() {
@@ -56,6 +57,15 @@ private long findBestScore() {
5657
.orElse(0);
5758
}
5859

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+
5969
private List<List<Integer>> generateCombinations(int total, int parts) {
6070
List<List<Integer>> result = new ArrayList<>();
6171
generateCombinationsHelper(total, parts, new ArrayList<>(), result);
@@ -100,6 +110,14 @@ private long calculateScore(List<Integer> amounts) {
100110
return (long) capacity * durability * flavor * texture;
101111
}
102112

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+
103121
private static class Ingredient {
104122
private final String name;
105123
private final int capacity;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /