-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay22.java
More file actions
358 lines (330 loc) · 9.52 KB
/
Day22.java
File metadata and controls
358 lines (330 loc) · 9.52 KB
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
import java.util.ArrayList;
public class Day22 {
public static void main(String[] args) {
new Day22();
}
public Day22() {
ArrayList<String> input = ReadInput.read("res/input22.txt");
partOne(input);
partTwo(input);
}
private void partOne(ArrayList<String> input) {
Tile[][] map = parseMap(input);
fixWraparounds(map);
ArrayList<Instruction> instructions = parseInstructions(input.get(input.size() - 1));
int facing = 0; // 0 = right, 1 = down, 2 = left, 3 = up
Tile current = findFirstTileInRow(map, 1);
for(Instruction instruction : instructions) {
if(instruction instanceof TurnInstruction)
facing = changeFacing(facing, ((TurnInstruction) instruction).turn);
else if(instruction instanceof MoveInstruction) {
MoveInstruction moveInstruction = (MoveInstruction) instruction;
for(int i = 0; i < moveInstruction.distance; i++) {
if(facing == 0) {
if(current.right != null && !(current.right instanceof WallTile))
current = current.right;
else
break;
}
else if(facing == 1) {
if(current.down != null && !(current.down instanceof WallTile))
current = current.down;
else
break;
}
else if(facing == 2) {
if(current.left != null && !(current.left instanceof WallTile))
current = current.left;
else
break;
}
else {
if(current.up != null && !(current.up instanceof WallTile))
current = current.up;
else
break;
}
}
}
}
System.out.println(1000 * current.y + 4 * current.x + facing);
}
int topX = 51;
int topY = 51;
int upX = 51;
int upY = 1;
int downX = 51;
int downY = 101;
int rightX = 101;
int rightY = 1;
int leftX = 1;
int leftY = 101;
int botX = 1;
int botY = 151;
private void partTwo(ArrayList<String> input) {
// OBS! Only works for my specific input.
Tile[][] map = parseMap(input);
fixWraparoundsPart2(map);
ArrayList<Instruction> instructions = parseInstructions(input.get(input.size() - 1));
int facing = 0; // 0 = right, 1 = down, 2 = left, 3 = up
Tile current = findFirstTileInRow(map, 1);
for(Instruction instruction : instructions) {
if(instruction instanceof TurnInstruction)
facing = changeFacing(facing, ((TurnInstruction) instruction).turn);
else if(instruction instanceof MoveInstruction) {
MoveInstruction moveInstruction = (MoveInstruction) instruction;
for(int i = 0; i < moveInstruction.distance; i++) {
if(facing == 0) {
if(current.right != null && !(current.right instanceof WallTile)) {
facing = updateFacingIfChangedCubeSide(facing, current.x, current.y);
current = current.right;
}
else
break;
}
else if(facing == 1) {
if(current.down != null && !(current.down instanceof WallTile)) {
facing = updateFacingIfChangedCubeSide(facing, current.x, current.y);
current = current.down;
}
else
break;
}
else if(facing == 2) {
if(current.left != null && !(current.left instanceof WallTile)) {
facing = updateFacingIfChangedCubeSide(facing, current.x, current.y);
current = current.left;
}
else
break;
}
else {
if(current.up != null && !(current.up instanceof WallTile)) {
facing = updateFacingIfChangedCubeSide(facing, current.x, current.y);
current = current.up;
}
else
break;
}
}
}
}
System.out.println(1000 * current.y + 4 * current.x + facing);
}
private int updateFacingIfChangedCubeSide(int facing, int x, int y) {
// up, left side
if(x == upX && y >= upY && y <= upY + 49 && facing == 2)
return 0;
// up, up side
else if(x >= upX && x <= upX + 49 && y == upY && facing == 3)
return 0;
// right, up side
//else if(x >= rightX && x <= rightX + 49 && y == rightY && facing == 3)
// return 3;
// right, right side
else if(x == rightX + 49 && y >= rightY && y <= rightY + 49 && facing == 0)
return 2;
// right, down side
else if(x >= rightX && x <= rightX + 49 && y == rightY + 49 && facing == 1)
return 2;
// top, left side
else if(x == topX && y >= topY && y <= topY + 49 && facing == 2)
return 1;
// top, right side
else if(x == topX + 49 && y >= topY && y <= topY + 49 && facing == 0)
return 3;
// down, right side
else if(x == downX + 49 && y >= downY && y <= downY + 49 && facing == 0)
return 2;
// down, down side
else if(x >= downX && x <= downX + 49 && y == downY + 49 && facing == 1)
return 2;
// left, up side
else if(x >= leftX && x <= leftX + 49 && y == leftY && facing == 3)
return 0;
// left, left side
else if(x == leftX && y >= leftY && y <= leftY + 49 && facing == 2)
return 0;
// bot, left side
else if(x == botX && y >= botY && y <= botY + 49 && facing == 2)
return 1;
//bot, bot side
//else if(x >= botX && x <= botX + 49 && y == botY + 49 && facing == 1)
// return 1;
//bot, right side
else if(x == botX + 49 && y >= botY && y <= botY + 49 && facing == 0)
return 3;
return facing;
}
private void fixWraparoundsPart2(Tile[][] map) {
for(int i = 0; i < 50; i++) {
// wrap up and bot
Tile upUpTile = map[upX + i][upY];
Tile botLeftTile = map[botX][botY + i];
upUpTile.up = botLeftTile;
botLeftTile.left = upUpTile;
// wrap up and left
Tile upLeftTile = map[upX][upY + i];
Tile leftLeftTile = map[leftX][leftY + 49 - i];
upLeftTile.left = leftLeftTile;
leftLeftTile.left = upLeftTile;
// wrap right and bot
Tile rightUpTile = map[rightX + i][rightY];
Tile botDownTile = map[botX + i][botY + 49];
rightUpTile.up = botDownTile;
botDownTile.down = rightUpTile;
// wrap right and down
Tile rightRightTile = map[rightX + 49][rightY + i];
Tile downRightTile = map[downX + 49][downY + 49 - i];
rightRightTile.right = downRightTile;
downRightTile.right = rightRightTile;
// wrap right and top
Tile rightDownTile = map[rightX + i][rightY + 49];
Tile topRightTile = map[topX + 49][topY + i];
rightDownTile.down = topRightTile;
topRightTile.right = rightDownTile;
// wrap top and left
Tile topLeftTile = map[topX][topY + i];
Tile leftTopTile = map[leftX + i][leftY];
topLeftTile.left = leftTopTile;
leftTopTile.up = topLeftTile;
//wrap down and bot
Tile downDownTile = map[downX + i][downY + 49];
Tile botRightTile = map[botX + 49][botY + i];
downDownTile.down = botRightTile;
botRightTile.right = downDownTile;
}
}
private Tile findFirstTileInRow(Tile[][] map, int row) {
for(int x = 0; x < map.length; x++) {
if(map[x][row] != null)
return map[x][row];
}
return null;
}
private int changeFacing(int currentFacing, char turn) {
int res = currentFacing;
if(turn == 'R')
res++;
else
res--;
res = res % 4;
return res < 0 ? res + 4 : res;
}
private Tile[][] parseMap(ArrayList<String> input) {
int height = input.size() - 2;
int width = 0;
for(int i = 0; i < input.size() - 2; i++) {
if(input.get(i).length() > width)
width = input.get(i).length();
}
Tile[][] map = new Tile[width + 2][height + 2];
for(int y = 1; y < height + 1; y++) {
for(int x = 1; x < width + 1; x++) {
if(x - 1 > input.get(y - 1).length() - 1 || input.get(y - 1).charAt(x - 1) == ' ')
continue;
else if(input.get(y - 1).charAt(x - 1) == '#') {
map[x][y] = new WallTile(x, y);
}
else {
Tile t = new Tile(x, y);
map[x][y] = t;
if(map[x - 1][y] != null)
map[x - 1][y].right = t;
if(map[x][y - 1] != null)
map[x][y - 1].down = t;
t.left = map[x - 1][y];
t.up = map[x][y - 1];
}
}
}
return map;
}
private void fixWraparounds(Tile[][] map) {
for(int y = 1; y < map[0].length - 1; y++) {
Tile first = null;
for(int x = 1; x < map.length - 1; x++) {
if(map[x][y] != null) {
first = map[x][y];
break;
}
}
Tile last = null;
for(int x = map.length - 2; x > 0; x--) {
if(map[x][y] != null) {
last = map[x][y];
break;
}
}
first.left = last;
last.right = first;
}
for(int x = 1; x < map.length - 1; x++) {
Tile first = null;
for(int y = 1; y < map[x].length - 1; y++) {
if(map[x][y] != null) {
first = map[x][y];
break;
}
}
Tile last = null;
for(int y = map[x].length - 2; y > 0; y--) {
if(map[x][y] != null) {
last = map[x][y];
break;
}
}
first.up = last;
last.down = first;
}
}
private ArrayList<Instruction> parseInstructions(String in) {
ArrayList<Instruction> res = new ArrayList<Instruction>();
for(int i = 0; i < in.length(); i++) {
int lastDigitIndex = i;
if(!Character.isDigit(in.charAt(i))) {
res.add(new TurnInstruction(in.charAt(i)));
continue;
}
else {
for(int j = i + 1; j < in.length(); j++) {
if(!Character.isDigit(in.charAt(j))) {
lastDigitIndex = j - 1;
break;
}
if(j == in.length() - 1)
lastDigitIndex = j;
}
res.add(new MoveInstruction(Integer.parseInt(in.substring(i, lastDigitIndex + 1))));
i = lastDigitIndex;
}
}
return res;
}
private interface Instruction {}
private class TurnInstruction implements Instruction {
public char turn;
public TurnInstruction(char turn) {
this.turn = turn;
}
}
private class MoveInstruction implements Instruction {
public int distance;
public MoveInstruction(int distance) {
this.distance = distance;
}
}
private class Tile {
public int x, y;
public Tile up, left, right, down;
public Tile(int x, int y) {
this.x = x;
this.y = y;
}
}
private class WallTile extends Tile {
public WallTile(int x, int y) {
super(x, y);
}
}
}