This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
/
Day20.java
437 lines (421 loc) · 16.3 KB
/
Day20.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package net.ddellspe.day20;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Day20 {
/**
* Reads in the map from the file and provides it in a streamable list of strings.
*
* @param fileName The name of the file (in the resource path) to read in
* @return the list of strings where each string is a line in the file
*/
public static List<String> readFile(String fileName) {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(Day20.class.getResourceAsStream(fileName)))) {
return reader.lines().collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Finds the product of the 4 corners of the final picture. This doesn't require actual generation
* of the picture in any way, as all Tiles with only 2 edges will be known to be the corners.
*
* @param fileName The name of the file (in the resource path) to read in
* @return the product of the 4 tile numbers in the corners.
*/
public static long part1(String fileName) {
List<String> data = readFile(fileName);
String tileString = data.get(0);
List<String> tileData = new ArrayList<>();
List<Tile> tiles = new ArrayList<>();
for (String row : data) {
if (row.startsWith("Tile")) {
tileString = row;
} else if (row.isBlank()) {
tiles.add(new Tile(tileString, tileData));
tileData = new ArrayList<>();
} else {
tileData.add(row);
}
}
tiles.add(new Tile(tileString, tileData));
Map<Integer, List<Integer>> tileMatches = new HashMap<>();
for (Tile tile : tiles) {
tileMatches.put(
tile.tileNumber,
tiles.stream()
.filter(tile2 -> tile.tileNumber != tile2.tileNumber)
.filter(
tile2 ->
tile.getEdges().stream()
.filter(
edge ->
tile2.getEdges().stream()
.filter(edge2 -> edge.equals(edge2))
.count()
> 0)
.count()
> 0)
.mapToInt(tile2 -> tile2.tileNumber)
.boxed()
.collect(Collectors.toList()));
}
return tileMatches.entrySet().stream()
.filter(entry -> entry.getValue().size() == 2)
.mapToLong(entry -> entry.getKey())
.reduce(1L, (a, b) -> a * b);
}
/**
* Completes part two of the Day 20 puzzle, this takes the input tile data, finds how how it's
* arranged (generall) with the general practice being based on the number of neighbors, so with
* 16 tiles, the neighbor count would be:
*
* <pre>
* 2 3 3 2
* 3 4 4 3
* 3 4 4 3
* 2 3 3 2
* </pre>
*
* Once the tile locations are found, orientation is set (from top-left to bottom right in the
* grid), by checking appropriate edges for equality with potential neighboring tiles and then
* making sure based on the grid location, the appropriate edges match (top left tile would be the
* bottom and right edge matching for instance).
*
* <p>Once the grid is locked and tiles are in place, the tiles are joined using the {@link
* Tile}'s edgeless data for the full puzzle size. This full puzzle is then rotated and/or flipped
* until the sea monster mask (below) is found within the puzzle. The final determination is that
* the number of matches for the mask (times the number of '#' in the mask) is removed from the
* total count of '#' in the final puzzle as the final result.
*
* <pre>
* #
* # ## ## ###
* # # # # # #
* </pre>
*
* @param fileName The name of the file (in the resource path) to read in
* @return the number of '#' in the final picture excluding those in the sea monster mask
*/
public static long part2(String fileName) {
List<String> data = readFile(fileName);
String tileString = data.get(0);
List<String> tileData = new ArrayList<>();
List<Tile> tiles = new ArrayList<>();
for (String row : data) {
if (row.startsWith("Tile")) {
tileString = row;
} else if (row.isBlank()) {
tiles.add(new Tile(tileString, tileData));
tileData = new ArrayList<>();
} else {
tileData.add(row);
}
}
tiles.add(new Tile(tileString, tileData));
Map<Integer, Tile> tilesByNumber =
tiles.stream().collect(Collectors.toMap(tile -> tile.tileNumber, tile -> tile));
Map<Integer, List<Integer>> tileMatches = new HashMap<>();
for (Tile tile : tiles) {
tileMatches.put(
tile.tileNumber,
tiles.stream()
.filter(tile2 -> tile.tileNumber != tile2.tileNumber)
.filter(
tile2 ->
tile.getEdges().stream()
.filter(
edge ->
tile2.getEdges().stream()
.filter(edge2 -> edge.equals(edge2))
.count()
> 0)
.count()
> 0)
.mapToInt(tile2 -> tile2.tileNumber)
.boxed()
.collect(Collectors.toList()));
}
// get the size of the board
final int size = (int) Math.sqrt(tiles.size() + 1);
int[][] board = new int[size][size];
// generate a map of the neighbor count and list of candidate tiles for that neighbor count
Map<Integer, List<Integer>> countToNeighbors = new HashMap<Integer, List<Integer>>();
IntStream.range(2, 5)
.forEach(
count ->
countToNeighbors.put(
count,
tileMatches.entrySet().stream()
.filter(entry -> entry.getValue().size() == count)
.map(entry -> entry.getKey())
.collect(Collectors.toList())));
// go through the entire board to lock tiles into a location, for location row==0 && col==0
// arbitrarily pick one of the 2-neighbor tiles to put in that location.
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (row == 0) {
if (col == 0) {
board[row][col] = countToNeighbors.get(2).get(0);
countToNeighbors.get(2).remove(0);
} else if (col == size - 1) {
board[row][col] =
tileMatches.get(board[row][col - 1]).stream()
.filter(val -> countToNeighbors.get(2).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(2).remove(Integer.valueOf(board[row][col]));
} else {
board[row][col] =
tileMatches.get(board[row][col - 1]).stream()
.filter(val -> countToNeighbors.get(3).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(3).remove(Integer.valueOf(board[row][col]));
}
} else if (row == size - 1) {
if (col == 0) {
board[row][col] =
tileMatches.get(board[row - 1][col]).stream()
.filter(val -> countToNeighbors.get(2).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(2).remove(Integer.valueOf(board[row][col]));
} else if (col == size - 1) {
board[row][col] = countToNeighbors.get(2).get(0);
countToNeighbors.get(2).remove(0);
} else {
board[row][col] =
tileMatches.get(board[row][col - 1]).stream()
.filter(val -> countToNeighbors.get(3).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(3).remove(Integer.valueOf(board[row][col]));
}
} else {
if (col == 0) {
board[row][col] =
tileMatches.get(board[row - 1][col]).stream()
.filter(val -> countToNeighbors.get(3).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(3).remove(Integer.valueOf(board[row][col]));
} else if (col == size - 1) {
board[row][col] =
tileMatches.get(board[row - 1][col]).stream()
.filter(val -> countToNeighbors.get(3).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(3).remove(Integer.valueOf(board[row][col]));
} else {
board[row][col] =
tileMatches.get(board[row - 1][col]).stream()
.filter(val -> countToNeighbors.get(4).contains(val))
.collect(Collectors.toList())
.get(0);
countToNeighbors.get(4).remove(Integer.valueOf(board[row][col]));
}
}
}
}
// rotate/flip tiles in their location to make joining all of the tiles together later better
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
Tile tile = tilesByNumber.get(board[row][col]);
int matches = 0;
do {
matches = 0;
// right
if (col == 0
&& tilesByNumber.get(board[row][col + 1]).getEdges().contains(tile.getEdges().get(0)))
matches++;
// left
if (col != 0
&& tilesByNumber.get(board[row][col - 1]).getEdges().contains(tile.getEdges().get(2)))
matches++;
if (matches < 1) {
tile.tileContents = rotateClockwise(tile.tileContents);
} else if (matches == 1) {
// bottom
if (row < size - 1
&& tilesByNumber
.get(board[row + 1][col])
.getEdges()
.contains(tile.getEdges().get(1))) matches++;
// top
if (row > 0
&& tilesByNumber
.get(board[row - 1][col])
.getEdges()
.contains(tile.getEdges().get(3))) matches++;
if (matches == 1) {
Collections.reverse(tile.tileContents);
}
}
} while (matches <= 1);
}
}
// Join the edgeless rows of the tiles together building upon the columns
List<String> finalImage =
IntStream.range(0, size)
.mapToObj(
row ->
IntStream.range(0, tilesByNumber.get(board[row][0]).getEdgelessData().size())
.mapToObj(
innerRow ->
IntStream.range(0, size)
.mapToObj(
col ->
tilesByNumber
.get(board[row][col])
.getEdgelessData()
.get(innerRow))
.reduce("", (a, b) -> (a + b)))
.collect(Collectors.toList()))
.flatMap(list -> list.stream())
.collect(Collectors.toList());
// Set the sea monster mask list
List<String> seaMonsterMask =
Arrays.asList(" # ", "# ## ## ###", " # # # # # # ");
boolean found = false;
int attempt = 0;
int monsterCount = 0;
// Rotate/flip the board until the monster is found in the puzzle, count the number of monsters
// found in the tiles
while (!found && attempt < 8) {
List<String> image = Collections.unmodifiableList(finalImage);
for (int row = 0; row < image.size() - seaMonsterMask.size(); row++) {
final int r = row;
for (int col = 0; col < image.get(0).length() - seaMonsterMask.get(0).length(); col++) {
final int c = col;
int goodRows = 0;
for (int innerRow = 0; innerRow < seaMonsterMask.size(); innerRow++) {
final int ir = innerRow;
String mask = seaMonsterMask.get(innerRow);
if (IntStream.range(0, mask.length())
.filter(ind -> mask.charAt(ind) == '#')
.filter(ind -> image.get(r + ir).charAt(c + ind) == '#')
.count()
!= IntStream.range(0, mask.length())
.filter(ind -> mask.charAt(ind) == '#')
.count()) {
break;
}
goodRows++;
}
if (goodRows == seaMonsterMask.size()) {
found = true;
monsterCount++;
}
}
}
if (found) {
break;
}
finalImage = rotateClockwise(finalImage);
attempt++;
// We've gone through all rotations, let's flip the order of the puzzle and start rotations
// again
if (attempt == 4) {
Collections.reverse(finalImage);
}
}
return (tiles.stream()
.flatMap(tile -> tile.getEdgelessData().stream())
.flatMap(str -> str.chars().boxed())
.filter(chr -> chr == (int) '#')
.count())
- (seaMonsterMask.stream()
.flatMap(str -> str.chars().boxed())
.filter(chr -> chr == (int) '#')
.count()
* monsterCount);
}
/**
* Rotates the List of Strings in a clockwise direction, for instance:
*
* <pre>
* 123
* 456
* 789
* </pre>
*
* becomes
*
* <pre>
* 741
* 852
* 963
* </pre>
*
* @param input
* @return
*/
public static List<String> rotateClockwise(List<String> input) {
return IntStream.range(0, input.size())
.mapToObj(
ind ->
input.stream()
.map(line -> new String("" + line.charAt(line.length() - ind - 1)))
.reduce("", (a, b) -> a + b))
.collect(Collectors.toList());
}
public static class Tile {
int tileNumber;
List<String> tileContents;
public Tile(String tileString, List<String> tileInput) {
tileNumber =
Integer.parseInt(
tileString.substring(tileString.indexOf(' ') + 1, tileString.indexOf(':')));
tileContents = tileInput.stream().collect(Collectors.toList());
}
/**
* Returns the string of all 4 edges in both forward and reverse. Order is right, bottom, left,
* top, right (reverse), bottom (reverse), left (reverse), top (reverse). This can be used to
* attempt to find neighboring tiles to this tile.
*
* @return a list of edges for this tile
*/
public List<String> getEdges() {
List<String> edges = new ArrayList<>();
edges.add(
tileContents.stream()
.map(line -> new String("" + line.charAt(line.length() - 1)))
.reduce("", (a, b) -> a + b)); // right
edges.add(
new StringBuilder(tileContents.get(tileContents.size() - 1))
.reverse()
.toString()); // bottom
edges.add(
tileContents.stream()
.map(line -> new String("" + line.charAt(0)))
.reduce("", (a, b) -> b + a)); // left
edges.add(tileContents.get(0)); // top
for (int i = 0; i < 4; i++) {
edges.add(new StringBuilder(edges.get(i)).reverse().toString());
}
return edges;
}
/**
* Returns the tile contents except for the data in the first row, last row, first column, and
* last column.
*
* @return the tile contents without first and last row or column
*/
public List<String> getEdgelessData() {
return IntStream.range(1, tileContents.size() - 1)
.mapToObj(ind -> tileContents.get(ind).substring(1, tileContents.get(ind).length() - 1))
.collect(Collectors.toList());
}
}
}