-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.groovy
326 lines (268 loc) · 8.17 KB
/
day23.groovy
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
import groovy.time.TimeCategory;
import groovy.time.TimeDuration;
import groovy.transform.EqualsAndHashCode;
import groovy.transform.ToString;
import groovy.transform.TupleConstructor;
@EqualsAndHashCode
@ToString
@TupleConstructor
class Pos {
final int x;
final int y;
}
@EqualsAndHashCode
class Map {
char[] arr;
int width;
int height;
Map(List<String> input) {
this.arr = input.join("").toCharArray();
this.width = input[0].size();
this.height = input.size();
}
def getAt(Pos pos) {
assert pos.x >= 0 && pos.x < width;
assert pos.y >= 0 && pos.y < height;
int arrpos = pos.x + pos.y * width;
return arr[arrpos];
}
def putAt(Pos pos, char value) {
assert pos.x >= 0 && pos.x < width;
assert pos.y >= 0 && pos.y < height;
int arrpos = pos.x + pos.y * width;
arr[arrpos] = value;
}
def posOf(char c) {
return arr.findIndexValues { it == c }.collect {
new Pos(it % width as int, Math.floor(it / width) as int)
};
}
def isInBounds(Pos pos) {
return pos.x >= 0 &&
pos.x < width &&
pos.y >= 0 &&
pos.y < height;
}
def isIntersection(Pos pos) {
def neighPaths = 0;
for (def next in [
new Pos(pos.x + 1, pos.y),
new Pos(pos.x - 1, pos.y),
new Pos(pos.x, pos.y + 1),
new Pos(pos.x, pos.y - 1),
]) {
if (isInBounds(next) && this[next] != '#') {
neighPaths += 1;
}
}
return neighPaths >= 3;
}
String toString() {
def result = "";
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result += this[new Pos(x, y)];
}
result += "\n";
}
return result;
}
}
@EqualsAndHashCode
@ToString
@TupleConstructor
class MapState {
final Pos pos;
final char dir;
final Pos from;
final int steps;
boolean unidir;
def nexts(Map map) {
def pos = this.pos;
def dir = this.dir;
def steps = this.steps;
assert map[pos] != '#' as char;
def result = [];
for (def next in [
new MapState(new Pos(pos.x + 1, pos.y), '>' as char, this.from, steps + 1, this.unidir),
new MapState(new Pos(pos.x - 1, pos.y), '<' as char, this.from, steps + 1, this.unidir),
new MapState(new Pos(pos.x, pos.y + 1), 'v' as char, this.from, steps + 1, this.unidir),
new MapState(new Pos(pos.x, pos.y - 1), '^' as char, this.from, steps + 1, this.unidir),
]) {
if (!map.isInBounds(next.pos)) {
continue;
}
if (dir == '^' as char && next.dir == 'v' as char
|| dir == 'v' as char && next.dir == '^' as char
|| dir == '<' as char && next.dir == '>' as char
|| dir == '>' as char && next.dir == '<' as char
) {
// No turning back.
continue;
}
if (map[next.pos] == '#' as char) {
// Can't walk here.
continue;
}
if (map[next.pos] == '.' as char) {
result << next;
continue;
}
def tile = map[next.pos];
def directionalTile = ['>' as char, '<' as char, '^' as char, 'v' as char].contains(tile);
if (directionalTile) {
if (tile == next.dir) {
next.unidir = true;
result << next;
}
continue;
}
assert false;
}
return result;
}
}
@EqualsAndHashCode
@ToString
@TupleConstructor
class Connection {
final int distance;
final boolean slippery;
}
class Graph {
private def connections = [:];
void add(long from, long to, Connection connection) {
if (connections[from] == null) {
connections[from] = [:];
}
if (connections[from][to] != null) {
assert "Connection from $from to $to already exists.";
}
connections[from][to] = connection;
}
Connection get(long from, long to) {
if (connections[from] == null) {
return null;
}
return connections[from][to];
}
def getConnections(long from) {
return connections[from];
}
def getNodes() {
def result = [] as Set;
connections.each {
result.add(it.key)
it.value.each {
result.add(it.key)
}
}
return result;
}
int size() {
return connections.values().collect { it.size() }.sum();
}
String toString() {
def result = "";
for (def from in connections.keySet()) {
for (def to in connections[from].keySet()) {
result += "${from} -> ${to}: ${connections[from][to]}\n";
}
}
return result;
}
}
def createGraph(def map) {
def graph = new Graph();
def nodeMasks = [:];
def nodeMaskIndex = 0;
def start = new Pos(1, 0);
def finish = new Pos(map.width - 2, map.height - 1);
assert map[start] == '.' as char;
assert map[finish] == '.' as char;
nodeMasks[start] = (0b1 as long) << nodeMaskIndex++;
nodeMasks[finish] = (0b1 as long) << nodeMaskIndex++;
assert nodeMasks[start] == 1;
assert nodeMasks[finish] == 2;
def frontier = [];
frontier << new MapState(start, 'v' as char, start, 0, false);
while (frontier.size() > 0) {
def curr = frontier.pop();
if (map.isIntersection(curr.pos) || curr.pos == finish) {
def from = curr.from;
def to = curr.pos;
if (nodeMasks[from] == null) {
nodeMasks[from] = (0b1 as long) << nodeMaskIndex++;
}
from = nodeMasks[from];
if (nodeMasks[to] == null) {
nodeMasks[to] = (0b1 as long) << nodeMaskIndex++;
}
to = nodeMasks[to];
graph.add(from, to, new Connection(curr.steps - 1, false));
graph.add(to, from, new Connection(curr.steps - 1, curr.unidir));
curr = new MapState(curr.pos, curr.dir, curr.pos, 0, false);
}
for (def next in curr.nexts(map)) {
frontier << next;
}
}
return graph;
}
@EqualsAndHashCode
@ToString
@TupleConstructor
class GraphState {
long pos;
long visited;
int distance;
}
def longestPath(Graph graph, long from, long to, boolean slippery) {
def frontier = [];
frontier << new GraphState(from, from, 0);
def finishStates = [];
while (frontier.size() > 0) {
GraphState curr = frontier.removeLast();
if (curr.pos == to) {
finishStates << curr;
continue;
}
for (def next in graph.getConnections(curr.pos)) {
long nextPos = next.key;
def nextConn = next.value;
if (slippery && nextConn.slippery) {
continue;
}
if ((curr.visited & nextPos) > 0) {
continue;
}
frontier << new GraphState(nextPos, curr.visited | nextPos, curr.distance + nextConn.distance);
}
}
return finishStates.collect { it.distance + Long.bitCount(it.visited) - 1 }.max();
}
def printElapsedTime(Closure closure) {
Date start = new Date();
closure();
Date stop = new Date();
println(TimeCategory.minus(stop, start));
}
def input = new File("input/day23.txt").readLines();
def map = new Map(input);
// println(map);
def graph;
printElapsedTime {
graph = createGraph(map);
}
// println(graph);
// println(graph.size());
def start = (0b1 as long) << 0;
def finish = (0b1 as long) << 1;
def oneBeforeFinish = graph.getConnections(finish).keySet()[0];
def distanceToFinish = graph.getConnections(finish).values()[0].distance + 1;
printElapsedTime {
println(longestPath(graph, start, oneBeforeFinish, true) + distanceToFinish);
}
printElapsedTime {
println(longestPath(graph, start, oneBeforeFinish, false) + distanceToFinish);
}