Skip to content

Commit d2d2ba6

Browse files
committed
Solved Day 15 (2015) - Science for Hungry People
1 parent 6223240 commit d2d2ba6

File tree

1 file changed

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

1 file changed

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

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.*;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
import java.util.stream.Collectors;
48

59
public class Day15 extends Day2015 {
6-
public Day15() {
7-
super(15);
8-
}
9-
10-
public static void main(String[] args) {
11-
new Day15().printParts();
12-
}
13-
14-
@Override
15-
public Object part1() {
16-
return "";
17-
}
18-
19-
@Override
20-
public Object part2() {
21-
return "";
22-
}
10+
11+
private static final int TOTAL_TEASPOONS = 100;
12+
private final List<Ingredient> ingredients = new ArrayList<>();
13+
14+
public Day15() {
15+
super(15);
16+
parseInput();
17+
}
18+
19+
public static void main(String[] args) {
20+
Day15 day = new Day15();
21+
day.printParts();
22+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 15, 1);
23+
}
24+
25+
private void parseInput() {
26+
Pattern pattern = Pattern.compile("(\\w+): capacity (-?\\d+), durability (-?\\d+), flavor (-?\\d+), texture (-?\\d+), calories (-?\\d+)");
27+
Arrays.stream(day().split("\n"))
28+
.map(pattern::matcher)
29+
.filter(Matcher::find)
30+
.map(matcher -> new Ingredient(
31+
matcher.group(1),
32+
Integer.parseInt(matcher.group(2)),
33+
Integer.parseInt(matcher.group(3)),
34+
Integer.parseInt(matcher.group(4)),
35+
Integer.parseInt(matcher.group(5)),
36+
Integer.parseInt(matcher.group(6))
37+
))
38+
.forEach(ingredients::add);
39+
}
40+
41+
@Override
42+
public Object part1() {
43+
return findBestScore();
44+
}
45+
46+
@Override
47+
public Object part2() {
48+
return 0; // Implement in next part
49+
}
50+
51+
private long findBestScore() {
52+
return generateCombinations(TOTAL_TEASPOONS, ingredients.size())
53+
.stream()
54+
.mapToLong(this::calculateScore)
55+
.max()
56+
.orElse(0);
57+
}
58+
59+
private List<List<Integer>> generateCombinations(int total, int parts) {
60+
List<List<Integer>> result = new ArrayList<>();
61+
generateCombinationsHelper(total, parts, new ArrayList<>(), result);
62+
return result;
63+
}
64+
65+
private void generateCombinationsHelper(int remaining, int parts, List<Integer> current, List<List<Integer>> result) {
66+
if (parts == 1) {
67+
List<Integer> combination = new ArrayList<>(current);
68+
combination.add(remaining);
69+
result.add(combination);
70+
return;
71+
}
72+
73+
for (int i = 0; i <= remaining; i++) {
74+
List<Integer> newCurrent = new ArrayList<>(current);
75+
newCurrent.add(i);
76+
generateCombinationsHelper(remaining - i, parts - 1, newCurrent, result);
77+
}
78+
}
79+
80+
private long calculateScore(List<Integer> amounts) {
81+
int capacity = 0;
82+
int durability = 0;
83+
int flavor = 0;
84+
int texture = 0;
85+
86+
for (int i = 0; i < ingredients.size(); i++) {
87+
Ingredient ingredient = ingredients.get(i);
88+
int amount = amounts.get(i);
89+
90+
capacity += ingredient.capacity * amount;
91+
durability += ingredient.durability * amount;
92+
flavor += ingredient.flavor * amount;
93+
texture += ingredient.texture * amount;
94+
}
95+
96+
if (capacity <= 0 || durability <= 0 || flavor <= 0 || texture <= 0) {
97+
return 0;
98+
}
99+
100+
return (long) capacity * durability * flavor * texture;
101+
}
102+
103+
private static class Ingredient {
104+
private final String name;
105+
private final int capacity;
106+
private final int durability;
107+
private final int flavor;
108+
private final int texture;
109+
private final int calories;
110+
111+
public Ingredient(String name, int capacity, int durability, int flavor, int texture, int calories) {
112+
this.name = name;
113+
this.capacity = capacity;
114+
this.durability = durability;
115+
this.flavor = flavor;
116+
this.texture = texture;
117+
this.calories = calories;
118+
}
119+
}
23120
}

0 commit comments

Comments
 (0)