-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.java
185 lines (145 loc) · 6.94 KB
/
Day15.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.belellou.kevin.advent.year2015;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ejml.data.DMatrixIterator;
import org.ejml.simple.SimpleMatrix;
import com.belellou.kevin.advent.generic.AbstractDaySolver;
@SuppressWarnings("unused")
public class Day15 extends AbstractDaySolver<Integer, Integer> {
private static final String GROUP_NAME = "name";
private static final String GROUP_CAPACITY = "capacity";
private static final String GROUP_DURABILITY = "durability";
private static final String GROUP_FLAVOR = "flavor";
private static final String GROUP_TEXTURE = "texture";
private static final String GROUP_CALORIES = "calories";
private static final Pattern PATTERN = Pattern.compile("^" +
"(?<" + GROUP_NAME + ">\\w+)" +
": capacity " +
"(?<" + GROUP_CAPACITY + ">-?\\d+)" +
", durability " +
"(?<" + GROUP_DURABILITY + ">-?\\d+)" +
", flavor " +
"(?<" + GROUP_FLAVOR + ">-?\\d+)" +
", texture " +
"(?<" + GROUP_TEXTURE + ">-?\\d+)" +
", calories " +
"(?<" + GROUP_CALORIES + ">-?\\d+)" +
"$");
private static Ingredient getIngredient(String line) {
Matcher matcher = PATTERN.matcher(line);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid line: " + line);
}
return new Ingredient(matcher);
}
private static SimpleMatrix getIngredientsMatrix(Collection<Ingredient> ingredients,
Function<Ingredient, double[]> toValues) {
double[][] data = ingredients.stream()
.map(toValues)
.toArray(double[][]::new);
return new SimpleMatrix(data);
}
private static List<double[][]> getRatioData() {
List<double[][]> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
for (int k = 0; k < 100; k++) {
list.add(new double[][] {{i, j, k, 100 - i - j - k}});
}
}
}
return list;
}
private static double getScore(SimpleMatrix matrix) {
return getScore(matrix, false);
}
private static double getScoreWithoutCalories(SimpleMatrix matrix) {
return getScore(matrix, true);
}
private static double getScore(SimpleMatrix matrix, boolean ignoreLastColum) {
double score = 0.0;
DMatrixIterator iterator = matrix.iterator(true, 0, 0, matrix.getNumRows() - 1,
ignoreLastColum ? matrix.getNumCols() - 2 : matrix.getNumCols() - 1);
while (iterator.hasNext()) {
double next = iterator.next();
if (next <= 0.0) {
score = 0.0;
break;
}
score = score == 0.0 ? next : score * next;
}
return score;
}
@Override
protected Integer doSolveFirstStar(BufferedReader reader) {
List<Ingredient> list = reader.lines()
.map(Day15::getIngredient)
.toList();
SimpleMatrix ingredientsMatrix = getIngredientsMatrix(list, Ingredient::getPartialValues);
List<double[][]> ratioData = getRatioData();
return (int) getRatioData().stream()
.map(SimpleMatrix::new)
.map(ratioMatrix -> ratioMatrix.mult(ingredientsMatrix))
.mapToDouble(Day15::getScore)
.max()
.orElseThrow();
}
@Override
public Integer getFirstStarSolution() {
return 18_965_440;
}
@Override
protected Integer doSolveSecondStar(BufferedReader reader) {
List<Ingredient> list = reader.lines()
.map(Day15::getIngredient)
.toList();
SimpleMatrix ingredientsMatrix = getIngredientsMatrix(list, Ingredient::getFullValues);
List<double[][]> ratioData = getRatioData();
return (int) getRatioData().stream()
.map(SimpleMatrix::new)
.map(ratioMatrix -> ratioMatrix.mult(ingredientsMatrix))
.filter(matrix -> matrix.get(0, matrix.getNumCols() - 1) == 500)
.mapToDouble(Day15::getScoreWithoutCalories)
.max()
.orElseThrow();
}
@Override
public Integer getSecondStarSolution() {
return 15_862_900;
}
private record Ingredient(String name, int capacity, int durability, int flavor, int texture, int calories) {
private Ingredient(Matcher matcher) {
this(getName(matcher), getCapacity(matcher), getDurability(matcher), getFlavor(matcher),
getTexture(matcher), getCalories(matcher));
}
private static String getName(Matcher matcher) {
return matcher.group(GROUP_NAME);
}
private static int getCapacity(Matcher matcher) {
return Integer.parseInt(matcher.group(GROUP_CAPACITY));
}
private static int getDurability(Matcher matcher) {
return Integer.parseInt(matcher.group(GROUP_DURABILITY));
}
private static int getFlavor(Matcher matcher) {
return Integer.parseInt(matcher.group(GROUP_FLAVOR));
}
private static int getTexture(Matcher matcher) {
return Integer.parseInt(matcher.group(GROUP_TEXTURE));
}
private static int getCalories(Matcher matcher) {
return Integer.parseInt(matcher.group(GROUP_CALORIES));
}
public double[] getPartialValues() {
return new double[] {capacity, durability, flavor, texture};
}
public double[] getFullValues() {
return new double[] {capacity, durability, flavor, texture, calories};
}
}
}