-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.java
279 lines (234 loc) · 10.2 KB
/
Day13.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
package aoc2018.day13;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
public class Day13 {
public static char[][] readInput(String inputFile, int horLength, int verLength) {
char[][] input = new char[verLength][horLength];
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(inputFile));
int yCoor = 0;
String line = reader.readLine();
while (yCoor < verLength) {
char[] arr = line.toCharArray();
for (int i = 0; i < input[yCoor].length; i++) {
input[yCoor][i] = i < arr.length ? arr[i] : ' ';
}
line = reader.readLine();
yCoor += 1;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
static List<Point> findInitialPositions(String inputFile, int horLength, int verLength) {
List<Point> points = new ArrayList<>();
char[][] matrix = readInput(inputFile, horLength, verLength);
for (int y = 0; y < verLength; y++) {
for (int x = 0; x < horLength; x++) {
if (matrix[y][x] == '^' || matrix[y][x] == '>' || matrix[y][x] == 'v' || matrix[y][x] == '<') {
points.add(new Point(x, y));
}
}
}
return points;
}
public static void printMatrix(char[][] matrix) {
for (char[] yChars : matrix) {
for (char xChar : yChars)
System.out.print(xChar);
System.out.println();
}
}
static Point findFirstCollisionPoint(char[][] matrix, List<Point> points) {
// Create initial cursors
List<Cursor> cursorList = getCursorList(matrix, points);
// Continue moving cursors until any two Points (coordinate pairs) overlap
while (areAllUnique(cursorList)) {
// Sort cursor list by y and then by x (increasing)
cursorList = cursorList.stream().sorted(Comparator
.comparing((Cursor c) -> c.currentPos.y)
.thenComparing((Cursor c) -> c.currentPos.x)
).collect(Collectors.toList());
List<Cursor> newCursorList = new ArrayList<>(cursorList);
// Loop through cursors
for (Cursor c : cursorList) {
Cursor newCur = getNextCursor(c, matrix);
// If moving a cursor results in overlapping another cursor, the latter represents the crash point
if (newCur.nextTurn == null)
return newCur.nextPos;
newCursorList.remove(c);
newCursorList.add(newCur);
}
cursorList = newCursorList;
}
return null;
}
static Point findLastRemainingCursor(char[][] matrix, List<Point> points) {
// Create initial cursors
List<Cursor> cursorList = getCursorList(matrix, points);
// Continue moving cursors until they crash two by two, and only one remains on the track
while (cursorList.size() > 1) {
cursorList = cursorList.stream().sorted(Comparator
.comparing((Cursor c) -> c.currentPos.y)
.thenComparing((Cursor c) -> c.currentPos.x)
).collect(Collectors.toList());
List<Cursor> newCursorList = new ArrayList<>(cursorList);
List<Cursor> collidingCursors = new ArrayList<>();
for (Cursor c : cursorList) {
// Skip the computation of any next cursor that has already collided (present in collidingCursors)
if (collidingCursors.stream().anyMatch(c::equals))
continue;
// If moving a cursor results in overlapping another cursor, the original track cells are restored
Cursor newCur = getNextCursor(c, matrix);
if (newCur.nextTurn == null) {
Cursor nextC = newCursorList.stream()
.filter(k -> k.currentPos.equals(newCur.nextPos))
.collect(Collectors.toList()).get(0);
matrix[nextC.currentPos.y][nextC.currentPos.x] = nextC.trackCellType;
// Store the colliding cursors
collidingCursors.add(nextC);
collidingCursors.add(c);
} else {
// Else, the cursors' position is stored in a list, which subsequently replaces the cursorList
newCursorList.remove(c);
newCursorList.add(newCur);
}
}
// Check if colliding cursors are present in the cursorList
cursorList = newCursorList;
List<Cursor> list = new ArrayList<>();
for (Cursor cc : cursorList) {
if (collidingCursors.stream().map(Cursor::getCurrentPos).anyMatch(c -> c.equals(cc.currentPos))) {
list.add(cc);
}
}
// Restore the track cells and remove any colliding cursors from cursorList
for (Cursor c : list) {
matrix[c.currentPos.y][c.currentPos.x] = c.trackCellType;
}
cursorList.removeAll(list);
}
// Return the only remaining cursor's coordinates
return cursorList.get(0).currentPos;
}
private static List<Cursor> getCursorList(char[][] matrix, List<Point> points) {
List<Cursor> cursorList = new ArrayList<>();
for (Point p : points) {
Cursor c1 = new Cursor(Direction.valueOf(matrix[p.y][p.x]), p, Turn.LEFT, '-');
c1.setTrackCellType((c1.direction == Direction.NORTH || c1.direction == Direction.SOUTH ? '|' : '-'));
cursorList.add(c1);
}
return cursorList;
}
static boolean areAllUnique(List<Cursor> list) {
Set<Point> set = new HashSet<>();
for (Cursor t : list)
if (!set.add(t.currentPos))
return false;
return true;
}
private static Cursor getNextCursor(Cursor c, char[][] matrix) {
Turn nextTurn = c.nextTurn;
// Handle cursor turn if the current cell is a +
if (c.trackCellType == '+') {
switch (c.nextTurn) {
case LEFT:
switch (c.direction.getDirChar()) {
case '^':
c.direction = Direction.valueOf('<');
break;
case '>':
c.direction = Direction.valueOf('^');
break;
case 'v':
c.direction = Direction.valueOf('>');
break;
case '<':
c.direction = Direction.valueOf('v');
break;
}
break;
case RIGHT:
switch (c.direction.getDirChar()) {
case '^':
c.direction = Direction.valueOf('>');
break;
case '>':
c.direction = Direction.valueOf('v');
break;
case 'v':
c.direction = Direction.valueOf('<');
break;
case '<':
c.direction = Direction.valueOf('^');
break;
}
break;
}
nextTurn = Turn.valueOf((c.nextTurn.getTurnVal() + 1) % 3);
}
// Handle cursor turn if the current cell is a turn ◜◝◞◟
switch (c.trackCellType) {
case '◜':
if (c.direction == Direction.NORTH)
c.direction = Direction.valueOf('>');
else
c.direction = Direction.valueOf('v');
break;
case '◝':
if (c.direction == Direction.NORTH)
c.direction = Direction.valueOf('<');
else
c.direction = Direction.valueOf('v');
break;
case '◞':
if (c.direction == Direction.SOUTH)
c.direction = Direction.valueOf('<');
else
c.direction = Direction.valueOf('^');
break;
case '◟':
if (c.direction == Direction.SOUTH)
c.direction = Direction.valueOf('>');
else
c.direction = Direction.valueOf('^');
break;
}
// Get coordinates of next cell
switch (c.direction) {
case NORTH:
c.setNextPos(new Point(c.currentPos.x, c.currentPos.y - 1));
break;
case SOUTH:
c.setNextPos(new Point(c.currentPos.x, c.currentPos.y + 1));
break;
case EAST:
c.setNextPos(new Point(c.currentPos.x + 1, c.currentPos.y));
break;
case WEST:
c.setNextPos(new Point(c.currentPos.x - 1, c.currentPos.y));
break;
}
// Get type of next cell
char nextCell = matrix[c.nextPos.y][c.nextPos.x];
// Check if next cell contains another cursor; if so, replace its track cell and return the current cursor
if (nextCell == '>' || nextCell == 'v' || nextCell == '<' || nextCell == '^') {
matrix[c.currentPos.y][c.currentPos.x] = c.trackCellType;
Cursor currC = new Cursor(c.direction, c.currentPos, null, c.trackCellType);
currC.setNextPos(c.nextPos);
return currC;
}
// Replace track cell, return next cursor
matrix[c.currentPos.y][c.currentPos.x] = c.trackCellType;
matrix[c.nextPos.y][c.nextPos.x] = c.direction.getDirChar();
return new Cursor(c.direction, c.nextPos, nextTurn, nextCell);
}
}