-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.groovy
278 lines (254 loc) · 9.23 KB
/
day10.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
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString;
import groovy.transform.TupleConstructor;
@EqualsAndHashCode
@ToString
@TupleConstructor
class Pos {
final int x;
final int y;
Pos dir(char d) {
switch(d) {
case "N": return new Pos(x , y - 1);
case "E": return new Pos(x + 1, y );
case "S": return new Pos(x , y + 1);
case "W": return new Pos(x - 1, y );
default:
assert false: "Invalid direction: $d";
}
}
Pos[] diadir(int d) {
// 0N1
// W E
// 3S2
switch(d) {
case 0: return [new Pos(x - 1, y - 1), new Pos(x , y - 1)];
case 1: return [new Pos(x + 1, y - 1), new Pos(x + 1, y )];
case 2: return [new Pos(x + 1, y + 1), new Pos(x , y + 1)];
case 3: return [new Pos(x - 1, y + 1), new Pos(x - 1, y )];
default:
assert false: "Invalid direction: $d";
}
}
}
class Tiles {
def arr;
def width;
def height;
Tiles(List<String> input) {
arr = input.join("").toCharArray();
width = input[0].size();
height = input.size();
}
def getAt(Pos pos) {
int arrpos = pos.x + pos.y * width;
return arr[arrpos];
}
def putAt(Pos pos, def val) {
int arrpos = pos.x + pos.y * width;
arr[arrpos] = val;
}
def positionOf(char c) {
def startPos = arr.findIndexOf { it == c };
return new Pos(startPos % width as int, Math.floor(startPos / width) as int);
}
def nextPositions(Pos pos) {
def x = pos.x;
def y = pos.y;
switch(this[pos]) {
case "|":
return [new Pos(x, y + 1), new Pos(x, y - 1)];
case "-":
return [new Pos(x + 1, y), new Pos(x - 1, y)];
case "F":
return [new Pos(x + 1, y), new Pos(x, y + 1)];
case "L":
return [new Pos(x + 1, y), new Pos(x, y - 1)];
case "J":
return [new Pos(x - 1, y), new Pos(x, y - 1)];
case "7":
return [new Pos(x - 1, y), new Pos(x, y + 1)];
default:
assert false;
}
}
def removeJunkPipes(def mainPipe) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (!mainPipe.contains(new Pos(x, y))) {
this[new Pos(x, y)] = "." as char;
}
}
}
}
def floodFill(Pos[] start, Closure condition, char value) {
def frontier = start.collect();
while (frontier.size() > 0) {
def curr = frontier.pop();
if (condition(this[curr])) {
this[curr] = value;
if (curr.x < this.width - 1) frontier.push(new Pos(curr.x + 1, curr.y));
if (curr.x > 0) frontier.push(new Pos(curr.x - 1, curr.y));
if (curr.y < this.height - 1) frontier.push(new Pos(curr.x, curr.y + 1));
if (curr.y > 0) frontier.push(new Pos(curr.x, curr.y - 1));
}
}
}
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;
}
}
def input = new File("input/day10.txt").readLines();
def tiles = new Tiles(input);
// Find starting position.
Pos startPos = tiles.positionOf("S" as char);
{ // Replace starting position with an appropriate pipe.
def northPipe = "|7F".toCharArray().findIndexOf { it == tiles[new Pos(startPos.x, startPos.y - 1)] } >= 0;
def southPipe = "|LJ".toCharArray().findIndexOf { it == tiles[new Pos(startPos.x, startPos.y + 1)] } >= 0;
def westPipe = "-LF".toCharArray().findIndexOf { it == tiles[new Pos(startPos.x - 1, startPos.y)] } >= 0;
def eastPipe = "-7J".toCharArray().findIndexOf { it == tiles[new Pos(startPos.x + 1, startPos.y)] } >= 0;
if (northPipe && southPipe) {
tiles[new Pos(startPos.x, startPos.y)] = "|" as char;
} else if (westPipe && eastPipe) {
tiles[new Pos(startPos.x, startPos.y)] = "-" as char;
} else if (northPipe && westPipe) {
tiles[new Pos(startPos.x, startPos.y)] = "J" as char;
} else if (northPipe && eastPipe) {
tiles[new Pos(startPos.x, startPos.y)] = "L" as char;
} else if (southPipe && westPipe) {
tiles[new Pos(startPos.x, startPos.y)] = "7" as char;
} else if (southPipe && eastPipe) {
tiles[new Pos(startPos.x, startPos.y)] = "F" as char;
} else {
assert false;
}
}
def pipeLoop = { // Part 1
def curr = startPos;
def direction =
tiles[curr] == "|" as char ? "N" :
tiles[curr] == "-" as char ? "E" :
tiles[curr] == "7" as char ? "W" :
tiles[curr] == "F" as char ? "E" :
tiles[curr] == "J" as char ? "N" :
tiles[curr] == "L" as char ? "E" :
null;
def loop = [];
while (loop[0] != curr) {
loop << curr;
curr = curr.dir(direction as char);
switch (direction) {
case "N":
if (tiles[curr] == "|" as char) {
direction = "N";
} else if (tiles[curr] == "F" as char) {
direction = "E";
} else if (tiles[curr] == "7" as char) {
direction = "W";
} else assert false;
break;
case "S":
if (tiles[curr] == "|" as char) {
direction = "S";
} else if (tiles[curr] == "L" as char) {
direction = "E";
} else if (tiles[curr] == "J" as char) {
direction = "W";
} else assert false;
break;
case "E":
if (tiles[curr] == "-" as char) {
direction = "E";
} else if (tiles[curr] == "7" as char) {
direction = "S";
} else if (tiles[curr] == "J" as char) {
direction = "N";
} else assert false;
break;
case "W":
if (tiles[curr] == "-" as char) {
direction = "W";
} else if (tiles[curr] == "L" as char) {
direction = "N";
} else if (tiles[curr] == "F" as char) {
direction = "S";
} else assert false;
break;
}
}
println(loop.size() / 2);
return loop;
}();
{ // Part 2
tiles.removeJunkPipes(pipeLoop);
{
def curr = tiles.positionOf("F" as char);
def direction = "E";
def innerDirection = 2;
def inner = [];
for (int i in 0..(pipeLoop.size())) {
curr.diadir(innerDirection).each { inner << it };
curr = curr.dir(direction as char);
switch (direction) {
case "N":
if (tiles[curr] == "|" as char) {
direction = "N";
} else if (tiles[curr] == "F" as char) {
direction = "E";
innerDirection += 1;
} else if (tiles[curr] == "7" as char) {
direction = "W";
innerDirection -= 1;
} else assert false;
break;
case "S":
if (tiles[curr] == "|" as char) {
direction = "S";
} else if (tiles[curr] == "L" as char) {
direction = "E";
innerDirection -= 1;
} else if (tiles[curr] == "J" as char) {
direction = "W";
innerDirection += 1;
} else assert false;
break;
case "E":
if (tiles[curr] == "-" as char) {
direction = "E";
} else if (tiles[curr] == "7" as char) {
direction = "S";
innerDirection += 1;
} else if (tiles[curr] == "J" as char) {
direction = "N";
innerDirection -= 1;
} else assert false;
break;
case "W":
if (tiles[curr] == "-" as char) {
direction = "W";
} else if (tiles[curr] == "L" as char) {
direction = "N";
innerDirection += 1;
} else if (tiles[curr] == "F" as char) {
direction = "S";
innerDirection -= 1;
} else assert false;
break;
}
innerDirection = (innerDirection + 4) % 4;
}
tiles.floodFill(
inner.findAll { tiles[it] == "." as char } as Pos[],
{ it == "." as char },
"X" as char
);
}
println(tiles.arr.findAll { it == "X" as char }.size());
}