-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.d
216 lines (184 loc) · 6.09 KB
/
day16.d
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
import std.stdio;
import std.exception;
import std.container : DList;
import std.typecons;
import std.format;
class GridLoc {
int row, col;
this(int r, int c) {
row = r;
col = c;
}
override size_t toHash() {
return row * 200 + col * 10;
}
override bool opEquals(Object o) {
GridLoc oth = cast(GridLoc) o;
return oth && row == oth.row && col == oth.col;
}
}
class Beam : GridLoc {
char dir;
this(int r, int c, char d) {
super(r, c);
dir = d;
}
override size_t toHash() {
return super.toHash() + (cast(int)dir) * 3000;
}
override bool opEquals(Object o) {
Beam oth = cast(Beam) o;
return oth && row == oth.row && col == oth.col && dir == oth.dir;
}
override string toString() {
return format("(%d,%d,%c)", this.row, this.col, this.dir);
}
};
string[] parse_grid(string filename) {
auto file = File(filename, "r");
string[] grid = new string[0];
string line;
while ((line = file.readln()) !is null) {
grid ~= line;
}
return grid;
}
// Eliminate duplicates that only differ by direction
ulong get_true_energized_count(bool[Beam] energized) {
bool[GridLoc] energized_pruned;
foreach (b; energized.keys) {
energized_pruned[new GridLoc(b.row, b.col)] = true;
}
return energized_pruned.length;
}
ulong part1(string filename) {
string[] grid = parse_grid(filename);
bool[Beam] energized;
get_full_path_iter(grid, new Beam(0, 0, 'r'), energized);
return get_true_energized_count(energized);
}
Beam get_next(string[] grid, Beam beam) {
Beam new_beam = null;
if (beam.dir == 'u' && beam.row > 0) {
new_beam = new Beam(beam.row - 1, beam.col, beam.dir);
} else if (beam.dir == 'l' && beam.col > 0) {
new_beam = new Beam(beam.row, beam.col - 1, beam.dir);
} else if (beam.dir == 'd' && beam.row < grid.length - 1) {
new_beam = new Beam(beam.row + 1, beam.col, beam.dir);
} else if (beam.dir == 'r' && beam.col < grid.length - 1) {
new_beam = new Beam(beam.row, beam.col + 1, beam.dir);
}
return new_beam;
}
void get_full_path_iter(string[] grid, Beam start, ref bool[Beam] energized) {
auto beams = DList!Beam(start);
void add_next(Beam beam) {
Beam new_beam = null;
if (beam.dir == 'u' && beam.row > 0) {
new_beam = new Beam(beam.row - 1, beam.col, beam.dir);
} else if (beam.dir == 'l' && beam.col > 0) {
new_beam = new Beam(beam.row, beam.col - 1, beam.dir);
} else if (beam.dir == 'd' && beam.row < grid.length - 1) {
new_beam = new Beam(beam.row + 1, beam.col, beam.dir);
} else if (beam.dir == 'r' && beam.col < grid.length - 1) {
new_beam = new Beam(beam.row, beam.col + 1, beam.dir);
}
auto exists = new_beam in energized;
if (new_beam !is null && exists is null) {
beams.insertBack(new_beam);
}
}
while (!beams.empty()) {
auto cur = beams.removeAny();
auto action = grid[cur.row][cur.col];
final switch (action) {
case '.':
add_next(cur);
break;
case '/':
final switch (cur.dir) {
case 'u':
add_next(new Beam(cur.row, cur.col, 'r'));
break;
case 'l':
add_next(new Beam(cur.row, cur.col, 'd'));
break;
case 'd':
add_next(new Beam(cur.row, cur.col, 'l'));
break;
case 'r':
add_next(new Beam(cur.row, cur.col, 'u'));
break;
}
break;
case '\\':
final switch (cur.dir) {
case 'u':
add_next(new Beam(cur.row, cur.col, 'l'));
break;
case 'l':
add_next(new Beam(cur.row, cur.col, 'u'));
break;
case 'd':
add_next(new Beam(cur.row, cur.col, 'r'));
break;
case 'r':
add_next(new Beam(cur.row, cur.col, 'd'));
break;
}
break;
case '-':
if (cur.dir == 'l' || cur.dir == 'r') {
add_next(cur);
} else {
add_next(new Beam(cur.row, cur.col, 'l'));
add_next(new Beam(cur.row, cur.col, 'r'));
}
break;
case '|':
if (cur.dir == 'u' || cur.dir == 'd') {
add_next(cur);
} else {
add_next(new Beam(cur.row, cur.col, 'u'));
add_next(new Beam(cur.row, cur.col, 'd'));
}
break;
}
energized[cur] = true;
}
}
ulong part2(string filename) {
string[] grid = parse_grid(filename);
ulong best = 0;
// Go through all of the grid entries
Beam[] starts = new Beam[grid.length * 4];
// top
for (int i = 0; i < grid.length; i++) {
starts[i] = new Beam(0, i, 'd');
}
// right
for (int i = 0; i < grid.length; i++) {
starts[grid.length + i] = new Beam(i, cast(int) grid.length - 1, 'l');
}
// bot
for (int i = 0; i < grid.length; i++) {
starts[grid.length * 2 + i] = new Beam(cast(int) grid.length - 1, i, 'u');
}
// left
for (int i = 0; i < grid.length; i++) {
starts[grid.length * 3 + i] = new Beam(i, 0, 'r');
}
foreach (start; starts) {
bool[Beam] energized;
get_full_path_iter(grid, start, energized);
ulong true_count = get_true_energized_count(energized);
if (true_count > best) {
best = true_count;
}
}
return best;
}
void main() {
writef("part1: %d\n", part1("day16.txt"));
writef("part2: %d\n", part2("day16.txt"));
}