-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.groovy
187 lines (166 loc) · 5.42 KB
/
day16.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
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(callSuper = true)
class Beam extends Pos {
final int dir;
Beam(int x, int y, int dir) {
super(x, y);
this.dir = dir;
}
String toString() {
return "Beam($x, $y, $dir)"
}
}
class Platform {
char[] arr;
int width;
int height;
Platform(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, char value) {
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)
};
}
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/day16.txt").readLines();
def platform = new Platform(input);
// println(platform);
Beam[] moveBeam(Platform platform, Beam beam) {
switch (beam.dir) {
case 0:
def np = new Pos(beam.x, beam.y - 1);
if (np.y < 0) {
return [];
}
if (platform[np] == '.' as char || platform[np] == '|' as char) {
return [new Beam(np.x, np.y, beam.dir)];
} else if (platform[np] == '\\' as char) {
return [new Beam(np.x, np.y, 3)];
} else if (platform[np] == '/' as char) {
return [new Beam(np.x, np.y, 1)];
} else if (platform[np] == '-' as char) {
return [new Beam(np.x, np.y, 1), new Beam(np.x, np.y, 3)];
} else {
assert false;
}
case 1:
def np = new Pos(beam.x + 1, beam.y);
if (np.x >= platform.width) {
return [];
}
if (platform[np] == '.' as char || platform[np] == '-' as char) {
return [new Beam(np.x, np.y, beam.dir)];
} else if (platform[np] == '\\' as char) {
return [new Beam(np.x, np.y, 2)];
} else if (platform[np] == '/' as char) {
return [new Beam(np.x, np.y, 0)];
} else if (platform[np] == '|' as char) {
return [new Beam(np.x, np.y, 0), new Beam(np.x, np.y, 2)];
} else {
assert false;
}
case 2:
def np = new Pos(beam.x, beam.y + 1);
if (np.y >= platform.height) {
return [];
}
if (platform[np] == '.' as char || platform[np] == '|' as char) {
return [new Beam(np.x, np.y, beam.dir)];
} else if (platform[np] == '\\' as char) {
return [new Beam(np.x, np.y, 1)];
} else if (platform[np] == '/' as char) {
return [new Beam(np.x, np.y, 3)];
} else if (platform[np] == '-' as char) {
return [new Beam(np.x, np.y, 1), new Beam(np.x, np.y, 3)];
} else {
assert false;
}
case 3:
def np = new Pos(beam.x - 1, beam.y);
if (np.x < 0) {
return [];
}
if (platform[np] == '.' as char || platform[np] == '-' as char) {
return [new Beam(np.x, np.y, beam.dir)];
} else if (platform[np] == '\\' as char) {
return [new Beam(np.x, np.y, 0)];
} else if (platform[np] == '/' as char) {
return [new Beam(np.x, np.y, 2)];
} else if (platform[np] == '|' as char) {
return [new Beam(np.x, np.y, 0), new Beam(np.x, np.y, 2)];
} else {
assert false;
}
default:
assert false : "Invalid direction";
}
}
int energize(Platform platform, Beam start) {
def beams = [start];
def energizedTiles = [:];
while (beams.size() > 0) {
def beam = beams.pop();
if (energizedTiles[beam] != null) {
continue;
}
energizedTiles[beam] = 1;
beams.addAll(moveBeam(platform, beam));
}
return energizedTiles.keySet()
.findAll { it != start }
.collectEntries { [new Pos(it.x, it.y), 1] }
.size();
}
def printElapsedTime(Closure closure) {
Date start = new Date();
closure();
Date stop = new Date();
println(TimeCategory.minus(stop, start));
}
printElapsedTime {
println(energize(platform, new Beam(-1, 0, 1)));
}
printElapsedTime {
def max = 0;
for (int i = 0; i < platform.width; i++) {
max = Math.max(max, energize(platform, new Beam(i, -1, 2)));
max = Math.max(max, energize(platform, new Beam(i, platform.height, 0)));
}
for (int i = 0; i < platform.height; i++) {
max = Math.max(max, energize(platform, new Beam(-1, i, 1)));
max = Math.max(max, energize(platform, new Beam(platform.width, i, 3)));
}
println(max);
}