-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23.java
412 lines (350 loc) · 15.3 KB
/
Day23.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
package net.eugenpaul.adventofcode.y2021.day23;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.eugenpaul.adventofcode.helper.Direction;
import net.eugenpaul.adventofcode.helper.SimplePos;
import net.eugenpaul.adventofcode.helper.SolutionTemplate;
import net.eugenpaul.adventofcode.helper.StringConverter;
import net.eugenpaul.adventofcode.helper.dijkstra.Dijkstra;
import net.eugenpaul.adventofcode.helper.dijkstra.DijkstraGen;
import net.eugenpaul.adventofcode.helper.dijkstra.DijkstraStepGen;
import net.eugenpaul.adventofcode.helper.dijkstra.Maze;
import net.eugenpaul.adventofcode.helper.dijkstra.MazeGen;
public class Day23 extends SolutionTemplate {
private class Step implements DijkstraStepGen {
private int cost = 0;
private final int depth;
private Character[][] stepArea;
public Step(int depth) {
this.depth = depth;
stepArea = new Character[13][this.depth];
for (int x = 0; x < 13; x++) {
Arrays.fill(stepArea[x], ' ');
}
}
public Step(Step b) {
this.depth = b.depth;
stepArea = new Character[13][this.depth];
for (int x = 0; x < 13; x++) {
System.arraycopy(b.stepArea[x], 0, stepArea[x], 0, this.depth);
}
}
@Override
public String compHash() {
StringBuilder response = new StringBuilder(70);
for (int x = 0; x < 13; x++) {
for (int y = 0; y < this.depth; y++) {
response.append(stepArea[x][y]);
}
}
return response.toString();
}
@Override
public int getCost() {
return cost;
}
public void print() {
for (int y = 0; y < this.depth; y++) {
for (int x = 0; x < 13; x++) {
System.out.print(stepArea[x][y]);
}
System.out.println("");
}
}
}
@AllArgsConstructor
private class SimpleMaze implements Maze {
private Set<SimplePos> area;
@Override
public List<SimplePos> getNextSteps(SimplePos from) {
List<SimplePos> response = new LinkedList<>();
for (Direction direction : Direction.values()) {
if (area.contains(from.moveNew(direction))) {
response.add(from.moveNew(direction));
}
}
return response;
}
}
private class BurrowMaze implements MazeGen<Step> {
private Set<SimplePos> area;
private List<SimplePos> hallwayStops = List.of(//
new SimplePos(1, 1), //
new SimplePos(2, 1), //
new SimplePos(4, 1), //
new SimplePos(6, 1), //
new SimplePos(8, 1), //
new SimplePos(10, 1), //
new SimplePos(11, 1) //
);
@Override
public List<Step> getNextSteps(Step from) {
Set<SimplePos> stepArea = new HashSet<>(area);
SimpleMaze simple = new SimpleMaze(stepArea);
Map<SimplePos, Character> fromRoomMap = new HashMap<>();
Map<SimplePos, Character> fromHallwayMap = new HashMap<>();
initStepData(from, stepArea, fromRoomMap, fromHallwayMap);
List<Step> response = new LinkedList<>();
for (var entry : fromRoomMap.entrySet()) {
for (var pos : hallwayStops) {
Dijkstra d = new Dijkstra();
Integer steps = d.getSteps(simple, entry.getKey(), pos);
if (steps != null) {
Step step = new Step(from);
setCost(step, entry, steps);
step.stepArea[entry.getKey().getX()][entry.getKey().getY()] = ' ';
step.stepArea[pos.getX()][pos.getY()] = entry.getValue();
response.add(step);
}
}
}
for (var entry : fromHallwayMap.entrySet()) {
List<SimplePos> roomStops = getToRoomStops(from, entry.getKey());
for (var pos : roomStops) {
Dijkstra d = new Dijkstra();
Integer steps = d.getSteps(simple, entry.getKey(), pos);
if (steps != null) {
Step step = new Step(from);
setCost(step, entry, steps);
step.stepArea[entry.getKey().getX()][entry.getKey().getY()] = ' ';
step.stepArea[pos.getX()][pos.getY()] = entry.getValue();
response.add(step);
}
}
}
return response;
}
private List<SimplePos> getToRoomStops(Step currentStep, SimplePos fromPos) {
Character c = currentStep.stepArea[fromPos.getX()][fromPos.getY()];
if (c == 'c') {
return Collections.emptyList();
}
int targetX;
switch (c) {
case 'A':
targetX = 3;
break;
case 'B':
targetX = 5;
break;
case 'C':
targetX = 7;
break;
case 'D':
targetX = 9;
break;
default:
throw new IllegalArgumentException();
}
if (fromPos.getX() == targetX) {
return Collections.emptyList();
}
List<SimplePos> response = new LinkedList<>();
if (currentStep.depth == 5) {
if (currentStep.stepArea[targetX][2] == ' ' && currentStep.stepArea[targetX][3].charValue() == c.charValue()) {
response.add(new SimplePos(targetX, 2));
} else if (currentStep.stepArea[targetX][2] == ' ' && currentStep.stepArea[targetX][3] == ' ') {
response.add(new SimplePos(targetX, 3));
}
} else {
if (currentStep.stepArea[targetX][2] == ' ' //
&& currentStep.stepArea[targetX][3].charValue() == c.charValue()//
&& currentStep.stepArea[targetX][4].charValue() == c.charValue()//
&& currentStep.stepArea[targetX][5].charValue() == c.charValue()//
) {
response.add(new SimplePos(targetX, 2));
} else if (currentStep.stepArea[targetX][2] == ' ' //
&& currentStep.stepArea[targetX][3] == ' ' //
&& currentStep.stepArea[targetX][4].charValue() == c.charValue()//
&& currentStep.stepArea[targetX][5].charValue() == c.charValue()//
) {
response.add(new SimplePos(targetX, 3));
} else if (currentStep.stepArea[targetX][2] == ' ' //
&& currentStep.stepArea[targetX][3] == ' ' //
&& currentStep.stepArea[targetX][4] == ' ' //
&& currentStep.stepArea[targetX][5].charValue() == c.charValue()//
) {
response.add(new SimplePos(targetX, 4));
} else if (currentStep.stepArea[targetX][2] == ' ' //
&& currentStep.stepArea[targetX][3] == ' ' //
&& currentStep.stepArea[targetX][4] == ' ' //
&& currentStep.stepArea[targetX][5] == ' ' //
) {
response.add(new SimplePos(targetX, 5));
}
}
return response;
}
private void setCost(Step step, Entry<SimplePos, Character> entry, Integer steps) {
switch (entry.getValue()) {
case 'A':
step.cost = steps;
break;
case 'B':
step.cost = steps * 10;
break;
case 'C':
step.cost = steps * 100;
break;
case 'D':
step.cost = steps * 1000;
break;
default:
throw new IllegalArgumentException();
}
}
private void initStepData(Step from, Set<SimplePos> stepArea, Map<SimplePos, Character> fromRoomMap, Map<SimplePos, Character> fromHallwayMap) {
// hallway
if (from.stepArea[1][1] != ' ') {
fromHallwayMap.put(new SimplePos(1, 1), from.stepArea[1][1]);
stepArea.remove(new SimplePos(1, 1));
}
if (from.stepArea[2][1] != ' ') {
fromHallwayMap.put(new SimplePos(2, 1), from.stepArea[2][1]);
stepArea.remove(new SimplePos(2, 1));
}
if (from.stepArea[4][1] != ' ') {
fromHallwayMap.put(new SimplePos(4, 1), from.stepArea[4][1]);
stepArea.remove(new SimplePos(4, 1));
}
if (from.stepArea[6][1] != ' ') {
fromHallwayMap.put(new SimplePos(6, 1), from.stepArea[6][1]);
stepArea.remove(new SimplePos(6, 1));
}
if (from.stepArea[8][1] != ' ') {
fromHallwayMap.put(new SimplePos(8, 1), from.stepArea[8][1]);
stepArea.remove(new SimplePos(8, 1));
}
if (from.stepArea[10][1] != ' ') {
fromHallwayMap.put(new SimplePos(10, 1), from.stepArea[10][1]);
stepArea.remove(new SimplePos(10, 1));
}
if (from.stepArea[11][1] != ' ') {
fromHallwayMap.put(new SimplePos(11, 1), from.stepArea[11][1]);
stepArea.remove(new SimplePos(11, 1));
}
// rooms
setFromRoomSteps(from, stepArea, fromRoomMap, 3, 'A');
setFromRoomSteps(from, stepArea, fromRoomMap, 5, 'B');
setFromRoomSteps(from, stepArea, fromRoomMap, 7, 'C');
setFromRoomSteps(from, stepArea, fromRoomMap, 9, 'D');
}
private void setFromRoomSteps(Step from, Set<SimplePos> stepArea, Map<SimplePos, Character> fromRoomMap, int x, char amphipod) {
if (from.depth == 5) {
if (from.stepArea[x][2] != ' ' && (from.stepArea[x][2] != amphipod || from.stepArea[x][3] != amphipod)) {
fromRoomMap.put(new SimplePos(x, 2), from.stepArea[x][2]);
stepArea.remove(new SimplePos(x, 2));
}
if (from.stepArea[x][3] != ' ' && (from.stepArea[x][3] != amphipod && from.stepArea[x][2] == ' ')) {
fromRoomMap.put(new SimplePos(x, 3), from.stepArea[x][3]);
stepArea.remove(new SimplePos(x, 3));
}
} else {
if (from.stepArea[x][2] != ' ' //
&& (from.stepArea[x][2] != amphipod || from.stepArea[x][3] != amphipod || from.stepArea[x][4] != amphipod
|| from.stepArea[x][5] != amphipod)) {
fromRoomMap.put(new SimplePos(x, 2), from.stepArea[x][2]);
stepArea.remove(new SimplePos(x, 2));
}
if (from.stepArea[x][3] != ' ' && from.stepArea[x][2] == ' '
&& (from.stepArea[x][3] != amphipod || from.stepArea[x][4] != amphipod || from.stepArea[x][5] != amphipod)) {
fromRoomMap.put(new SimplePos(x, 3), from.stepArea[x][3]);
stepArea.remove(new SimplePos(x, 3));
}
if (from.stepArea[x][4] != ' ' && from.stepArea[x][3] == ' ' && from.stepArea[x][2] == ' '
&& (from.stepArea[x][4] != amphipod || from.stepArea[x][5] != amphipod)) {
fromRoomMap.put(new SimplePos(x, 4), from.stepArea[x][4]);
stepArea.remove(new SimplePos(x, 4));
}
if (from.stepArea[x][5] != ' ' && from.stepArea[x][4] == ' ' && from.stepArea[x][3] == ' ' && from.stepArea[x][2] == ' '
&& from.stepArea[x][5] != amphipod) {
fromRoomMap.put(new SimplePos(x, 5), from.stepArea[x][5]);
stepArea.remove(new SimplePos(x, 5));
}
}
}
}
@Getter
private long totalEnergy;
@Getter
private long totalEnergy2;
public static void main(String[] args) {
Day23 puzzle = new Day23();
puzzle.doPuzzleFromFile("y2021/day23/puzzle1.txt");
}
@Override
public boolean doEvent(List<String> eventData) {
totalEnergy = doPuzzle1(eventData);
totalEnergy2 = doPuzzle2(eventData);
logger.log(Level.INFO, () -> "totalEnergy : " + getTotalEnergy());
logger.log(Level.INFO, () -> "totalEnergy2 : " + getTotalEnergy2());
return true;
}
private int doPuzzle1(List<String> eventData) {
Set<SimplePos> area = StringConverter.toSet(eventData, 'A', 'B', 'C', 'D', '.');
BurrowMaze maze = new BurrowMaze();
maze.area = area;
Step from = new Step(5);
from.stepArea[3][2] = eventData.get(2).charAt(3);
from.stepArea[3][3] = eventData.get(3).charAt(3);
from.stepArea[5][2] = eventData.get(2).charAt(5);
from.stepArea[5][3] = eventData.get(3).charAt(5);
from.stepArea[7][2] = eventData.get(2).charAt(7);
from.stepArea[7][3] = eventData.get(3).charAt(7);
from.stepArea[9][2] = eventData.get(2).charAt(9);
from.stepArea[9][3] = eventData.get(3).charAt(9);
Step to = new Step(5);
to.stepArea[3][2] = 'A';
to.stepArea[3][3] = 'A';
to.stepArea[5][2] = 'B';
to.stepArea[5][3] = 'B';
to.stepArea[7][2] = 'C';
to.stepArea[7][3] = 'C';
to.stepArea[9][2] = 'D';
to.stepArea[9][3] = 'D';
return DijkstraGen.getSteps(maze, from, to);
}
private Integer doPuzzle2(List<String> eventData) {
List<String> newEventData = new LinkedList<>();
newEventData.add(eventData.get(0));
newEventData.add(eventData.get(1));
newEventData.add(eventData.get(2));
newEventData.add(" #D#C#B#A# ");
newEventData.add(" #D#B#A#C# ");
newEventData.add(eventData.get(3));
newEventData.add(eventData.get(4));
Set<SimplePos> area = StringConverter.toSet(newEventData, 'A', 'B', 'C', 'D', '.');
BurrowMaze maze = new BurrowMaze();
maze.area = area;
Step from = new Step(7);
for (int x = 3; x <= 9; x = x + 2) {
for (int y = 2; y <= 5; y++) {
from.stepArea[x][y] = newEventData.get(y).charAt(x);
}
}
Step to = new Step(7);
for (int y = 2; y <= 5; y++) {
to.stepArea[3][y] = 'A';
}
for (int y = 2; y <= 5; y++) {
to.stepArea[5][y] = 'B';
}
for (int y = 2; y <= 5; y++) {
to.stepArea[7][y] = 'C';
}
for (int y = 2; y <= 5; y++) {
to.stepArea[9][y] = 'D';
}
return DijkstraGen.getSteps(maze, from, to);
}
}