-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionTest.java
280 lines (243 loc) · 11.6 KB
/
SolutionTest.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.aoc.year2023.day02;
import com.aoc.year2023.utils.FileProcessing;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
private static final String resourcesTestURI = "src/test/resources/";
private static final String inputFileURI = "src/main/resources/year2023/day02/input.txt";
private static final String testTask1URI = resourcesTestURI + "year2023/day02/test_1.txt";
private Solution solution;
@BeforeEach
void setUp() {
solution = new Solution();
}
@ParameterizedTest
@MethodSource("solveMinimumPowerDataSource")
void testSolveMinimumPower(int expectedPowerResult, String inputFilePath) {
String absolutePath = FileProcessing.getAbsolutePathFromUriString(inputFilePath);
List<String> puzzleData = FileProcessing.readFile(absolutePath);
int actualPowerResult = solution.solveMinimumPower(puzzleData, false);
assertEquals(expectedPowerResult, actualPowerResult);
}
@ParameterizedTest
@MethodSource("solvePossibleGamesDataSource")
void testSolvePossibleGames(int expectedSumResult, String inputFilePath) {
String absolutePath = FileProcessing.getAbsolutePathFromUriString(inputFilePath);
List<String> puzzleData = FileProcessing.readFile(absolutePath);
int actualSumResult = solution.solvePossibleGames(puzzleData, false);
assertEquals(expectedSumResult, actualSumResult);
}
@ParameterizedTest
@MethodSource("processCubeDataFromGameDataSource")
void testProcessCubeDataFromGame(String inputs, String[] expectedResult) {
String[] actualResult = solution.processCubeDataFromGame(inputs, false);
assertArrayEquals(expectedResult, actualResult);
}
@ParameterizedTest
@MethodSource("computePowerForCubesDataSource")
void testComputePowerForCubes(Map<String, Integer> inputs, int expectedResult) {
int actualResult = solution.computePowerForCubes(inputs);
assertEquals(expectedResult, actualResult);
}
@ParameterizedTest
@MethodSource("computeMinimumCubesPerColorDataSource")
void testComputeMinimumCubesPerColor(String[] inputs, Map<String, Integer> expectedResult) {
Map<String, Integer> actualResult = solution.computeMinimumCubesPerColor(inputs);
assertEquals(expectedResult, actualResult);
}
@ParameterizedTest
@MethodSource("getNumCubesOfSetDataSource")
void testGetNumCubesOfSet(String[] inputs, Map<String, Integer> expectedResult) {
Map<String, Integer> actualResult = solution.getNumCubesOfSet(inputs);
assertEquals(expectedResult, actualResult);
}
@ParameterizedTest
@MethodSource("getImpossibilityConfigurationsNumDataSource")
void testGetImpossibilityConfigurationsNum(String[] data, int expectedImpossibleConfigs) {
int actualImpossibleConfigs = solution.getImpossibilityConfigurationsNum(data, false);
assertEquals(expectedImpossibleConfigs, actualImpossibleConfigs);
}
@ParameterizedTest
@MethodSource("checkNumberThresholdDataSource")
void testCheckNumberThreshold(String[] data, boolean expectedPossibility) {
boolean actualPossibility = solution.checkNumberThreshold(data);
assertEquals(expectedPossibility, actualPossibility);
}
@ParameterizedTest
@MethodSource("convertBoolToPossibilityDataSource")
void testConvertBoolToPossibility(String expectedResult, boolean isPossible) {
String actualResult = solution.convertBoolToPossibility(isPossible);
assertEquals(expectedResult, actualResult);
}
private static Stream<Arguments> solveMinimumPowerDataSource() {
return Stream.of(
Arguments.of(71220, inputFileURI),
Arguments.of(2286, testTask1URI)
);
}
private static Stream<Arguments> solvePossibleGamesDataSource() {
return Stream.of(
Arguments.of(2377, inputFileURI),
Arguments.of(8, testTask1URI)
);
}
private static Stream<Arguments> processCubeDataFromGameDataSource() {
return Stream.of(
Arguments.of(
"Game 1: 1 green, 1 blue, 1 red; 3 green, 1 blue, 1 red; 4 green, 3 blue, 1 red; 4 green, 2 blue, 1 red; 3 blue, 3 green",
new String[]{
" 1 green, 1 blue, 1 red",
" 3 green, 1 blue, 1 red",
" 4 green, 3 blue, 1 red",
" 4 green, 2 blue, 1 red",
" 3 blue, 3 green"
}
),
Arguments.of(
"Game 20: 7 blue; 1 blue, 6 green, 1 red; 1 red, 3 blue, 10 green; 7 green, 4 blue, 1 red; 6 green, 6 blue, 1 red; 1 red, 5 blue, 17 green",
new String[]{
" 7 blue",
" 1 blue, 6 green, 1 red",
" 1 red, 3 blue, 10 green",
" 7 green, 4 blue, 1 red",
" 6 green, 6 blue, 1 red",
" 1 red, 5 blue, 17 green"
}
)
);
}
private static Stream<Arguments> computePowerForCubesDataSource() {
return Stream.of(
Arguments.of(Map.ofEntries(
Map.entry("red", 53),
Map.entry("green", 4),
Map.entry("blue", 3)
), 636),
Arguments.of(Map.ofEntries(
Map.entry("red", 43),
Map.entry("green", 4),
Map.entry("blue", 3)
), 516),
Arguments.of(Map.ofEntries(
Map.entry("red", 1),
Map.entry("green", 99),
Map.entry("blue", 3)
), 297),
Arguments.of(Map.ofEntries(
Map.entry("red", 420),
Map.entry("green", 420),
Map.entry("blue", 420)
), 74088000)
);
}
private static Stream<Arguments> computeMinimumCubesPerColorDataSource() {
return Stream.of(
Arguments.of(new String[]{
" 1 green, 1 blue, 1 red",
" 3 green, 1 blue, 1 red",
" 4 green, 3 blue, 53 red",
" 4 green, 2 blue, 1 red",
" 3 blue, 3 green"}, Map.ofEntries(
Map.entry("red", 53),
Map.entry("green", 4),
Map.entry("blue", 3)
)),
Arguments.of(new String[]{
" 1 green, 1 blue, 1 red",
" 3 green, 1 blue, 42 red",
" 4 green, 3 blue, 42 red",
" 4 green, 2 blue, 43 red",
" 3 blue, 3 green"}, Map.ofEntries(
Map.entry("red", 43),
Map.entry("green", 4),
Map.entry("blue", 3)
)),
Arguments.of(new String[]{
" 1 green, 1 blue, 1 red",
" 3 green, 1 blue, 1 red",
" 4 green, 3 blue, 1 red",
" 4 green, 2 blue, 1 red",
" 3 blue, 99 green"}, Map.ofEntries(
Map.entry("red", 1),
Map.entry("green", 99),
Map.entry("blue", 3)
)),
Arguments.of(new String[]{
" 1 green, 420 blue, 1 red",
" 3 green, 1 blue, 420 red",
" 420 green, 3 blue, 1 red",
" 4 green, 2 blue, 1 red",
" 420 blue, 420 green"}, Map.ofEntries(
Map.entry("red", 420),
Map.entry("green", 420),
Map.entry("blue", 420)
))
);
}
private static Stream<Arguments> getNumCubesOfSetDataSource() {
return Stream.of(
Arguments.of(new String[]{" 1 green", " 1 blue", " 1 red"}, Map.ofEntries(
Map.entry("red", 1),
Map.entry("green", 1),
Map.entry("blue", 1)
)),
Arguments.of(new String[]{" 45 green", " 81 blue", " 23 red"}, Map.ofEntries(
Map.entry("red", 23),
Map.entry("green", 45),
Map.entry("blue", 81)
)),
Arguments.of(new String[]{" 5 green", " 5 blue", " 3 red"}, Map.ofEntries(
Map.entry("green", 5),
Map.entry("red", 3),
Map.entry("blue", 5)
)),
Arguments.of(new String[]{" 42 green", " 42 blue", " 42 red"}, Map.ofEntries(
Map.entry("green", 42),
Map.entry("blue", 42),
Map.entry("red", 42)
))
);
}
private static Stream<Arguments> getImpossibilityConfigurationsNumDataSource() {
return Stream.of(
Arguments.of(new String[]{
"10 blue, 2 red", "7 green, 20 blue, 9 red", " 8 red, 6 green, 2 blue"}, 1),
Arguments.of(new String[]{
"10 blue, 2 red", "22 green, 20 blue, 999 red", " 8 red, 6 green, 2 blue"}, 1),
Arguments.of(new String[]{
"10 blue, 2 red", "22 green, 20 blue, 999 red", " 13 red, 6 green, 2 blue"}, 2),
Arguments.of(new String[]{
"15 blue, 2 red", "7 green, 20 blue, 9 red", " 99 red, 6 green, 2 blue"}, 3),
Arguments.of(new String[]{
"10 blue, 2 red", "7 green, 3 blue, 9 red", " 8 red, 6 green, 2 blue"}, 0)
);
}
private static Stream<Arguments> checkNumberThresholdDataSource() {
return Stream.of(
Arguments.of(new String[]{" 1 red", " 2 green", " 99 blue"}, false),
Arguments.of(new String[]{" 42 red", " 2 green", " 99 blue"}, false),
Arguments.of(new String[]{" 1 red", " 420 green", " 99 blue"}, false),
Arguments.of(new String[]{" 42 red ", " 420 green", " 99 blue"}, false),
Arguments.of(new String[]{" 1 red", " 2 green", " 3 blue "}, true),
Arguments.of(new String[]{"10 red", "11 green", "12 blue"}, true),
Arguments.of(new String[]{"12 red", "13 green", "14 blue"}, true),
Arguments.of(new String[]{"7 blue", "5 green", "7 red"}, true),
Arguments.of(new String[]{"7 blue", "5 red", "7 green"}, true),
Arguments.of(new String[]{"7 green", "5 blue", "7 red"}, true),
Arguments.of(new String[]{"7 green", "5 red", "7 blue"}, true)
);
}
private static Stream<Arguments> convertBoolToPossibilityDataSource() {
return Stream.of(
Arguments.of("possible", true),
Arguments.of("impossible", false)
);
}
}