Skip to content

Commit 73b985d

Browse files
committed
Day 16
1 parent fd78f4c commit 73b985d

File tree

2 files changed

+297
-0
lines changed

2 files changed

+297
-0
lines changed

day16.groovy

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import groovy.time.TimeCategory;
2+
import groovy.time.TimeDuration;
3+
import groovy.transform.EqualsAndHashCode;
4+
import groovy.transform.ToString;
5+
import groovy.transform.TupleConstructor;
6+
7+
8+
@EqualsAndHashCode
9+
@ToString
10+
@TupleConstructor
11+
class Pos {
12+
final int x;
13+
final int y;
14+
}
15+
16+
@EqualsAndHashCode(callSuper = true)
17+
class Beam extends Pos {
18+
final int dir;
19+
Beam(int x, int y, int dir) {
20+
super(x, y);
21+
this.dir = dir;
22+
}
23+
24+
String toString() {
25+
return "Beam($x, $y, $dir)"
26+
}
27+
}
28+
29+
class Platform {
30+
char[] arr;
31+
int width;
32+
int height;
33+
34+
Platform(List<String> input) {
35+
arr = input.join("").toCharArray();
36+
width = input[0].size();
37+
height = input.size();
38+
}
39+
40+
def getAt(Pos pos) {
41+
int arrpos = pos.x + pos.y * width;
42+
return arr[arrpos];
43+
}
44+
45+
def putAt(Pos pos, char value) {
46+
int arrpos = pos.x + pos.y * width;
47+
arr[arrpos] = value;
48+
}
49+
50+
def posOf(char c) {
51+
return arr.findIndexValues { it == c }.collect {
52+
new Pos(it % width as int, Math.floor(it / width) as int)
53+
};
54+
}
55+
56+
String toString() {
57+
def result = "";
58+
for (int y = 0; y < height; y++) {
59+
for (int x = 0; x < width; x++) {
60+
result += this[new Pos(x, y)];
61+
}
62+
result += "\n";
63+
}
64+
return result;
65+
}
66+
}
67+
68+
69+
def input = new File("input/day16.txt").readLines();
70+
def platform = new Platform(input);
71+
// println(platform);
72+
73+
Beam[] moveBeam(Platform platform, Beam beam) {
74+
switch (beam.dir) {
75+
case 0:
76+
def np = new Pos(beam.x, beam.y - 1);
77+
if (np.y < 0) {
78+
return [];
79+
}
80+
if (platform[np] == '.' as char || platform[np] == '|' as char) {
81+
return [new Beam(np.x, np.y, beam.dir)];
82+
} else if (platform[np] == '\\' as char) {
83+
return [new Beam(np.x, np.y, 3)];
84+
} else if (platform[np] == '/' as char) {
85+
return [new Beam(np.x, np.y, 1)];
86+
} else if (platform[np] == '-' as char) {
87+
return [new Beam(np.x, np.y, 1), new Beam(np.x, np.y, 3)];
88+
} else {
89+
assert false;
90+
}
91+
case 1:
92+
def np = new Pos(beam.x + 1, beam.y);
93+
if (np.x >= platform.width) {
94+
return [];
95+
}
96+
if (platform[np] == '.' as char || platform[np] == '-' as char) {
97+
return [new Beam(np.x, np.y, beam.dir)];
98+
} else if (platform[np] == '\\' as char) {
99+
return [new Beam(np.x, np.y, 2)];
100+
} else if (platform[np] == '/' as char) {
101+
return [new Beam(np.x, np.y, 0)];
102+
} else if (platform[np] == '|' as char) {
103+
return [new Beam(np.x, np.y, 0), new Beam(np.x, np.y, 2)];
104+
} else {
105+
assert false;
106+
}
107+
case 2:
108+
def np = new Pos(beam.x, beam.y + 1);
109+
if (np.y >= platform.height) {
110+
return [];
111+
}
112+
if (platform[np] == '.' as char || platform[np] == '|' as char) {
113+
return [new Beam(np.x, np.y, beam.dir)];
114+
} else if (platform[np] == '\\' as char) {
115+
return [new Beam(np.x, np.y, 1)];
116+
} else if (platform[np] == '/' as char) {
117+
return [new Beam(np.x, np.y, 3)];
118+
} else if (platform[np] == '-' as char) {
119+
return [new Beam(np.x, np.y, 1), new Beam(np.x, np.y, 3)];
120+
} else {
121+
assert false;
122+
}
123+
case 3:
124+
def np = new Pos(beam.x - 1, beam.y);
125+
if (np.x < 0) {
126+
return [];
127+
}
128+
if (platform[np] == '.' as char || platform[np] == '-' as char) {
129+
return [new Beam(np.x, np.y, beam.dir)];
130+
} else if (platform[np] == '\\' as char) {
131+
return [new Beam(np.x, np.y, 0)];
132+
} else if (platform[np] == '/' as char) {
133+
return [new Beam(np.x, np.y, 2)];
134+
} else if (platform[np] == '|' as char) {
135+
return [new Beam(np.x, np.y, 0), new Beam(np.x, np.y, 2)];
136+
} else {
137+
assert false;
138+
}
139+
default:
140+
assert false : "Invalid direction";
141+
}
142+
}
143+
144+
int energize(Platform platform, Beam start) {
145+
def beams = [start];
146+
def energizedTiles = [:];
147+
while (beams.size() > 0) {
148+
def beam = beams.pop();
149+
if (energizedTiles[beam] != null) {
150+
continue;
151+
}
152+
energizedTiles[beam] = 1;
153+
154+
beams.addAll(moveBeam(platform, beam));
155+
}
156+
157+
return energizedTiles.keySet()
158+
.findAll { it != start }
159+
.collectEntries { [new Pos(it.x, it.y), 1] }
160+
.size();
161+
}
162+
163+
def printElapsedTime(Closure closure) {
164+
Date start = new Date();
165+
closure();
166+
Date stop = new Date();
167+
println(TimeCategory.minus(stop, start));
168+
}
169+
170+
printElapsedTime {
171+
println(energize(platform, new Beam(-1, 0, 1)));
172+
}
173+
174+
printElapsedTime {
175+
def max = 0;
176+
for (int i = 0; i < platform.width; i++) {
177+
max = Math.max(max, energize(platform, new Beam(i, -1, 2)));
178+
max = Math.max(max, energize(platform, new Beam(i, platform.height, 0)));
179+
}
180+
181+
for (int i = 0; i < platform.height; i++) {
182+
max = Math.max(max, energize(platform, new Beam(-1, i, 1)));
183+
max = Math.max(max, energize(platform, new Beam(platform.width, i, 3)));
184+
}
185+
186+
println(max);
187+
}

input/day16.txt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
......../.|.................|......|.|.-....|...................../....................................\......

0 commit comments

Comments
 (0)