-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.cpp
376 lines (343 loc) · 11.3 KB
/
15.cpp
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "precompiled.h"
#include "com.h"
#include "d2d.h"
#include "intcode.h"
constexpr int starti = 19;
constexpr int startj = 21;
constexpr int width = 41;
constexpr int height = 41;
constexpr int scale = 8;
constexpr int line_width = 3;
constexpr int explore_speed = 4;
constexpr int snake_length = 8;
constexpr int oxygen_speed = 3;
constexpr int oxygen_length = 5;
constexpr float scalef = (float)scale;
enum contents_t { unknown, wall, home, empty, oxygen, droid, pump, path };
struct tile_t {
contents_t contents;
int distance;
};
using line_t = std::array<tile_t, width>;
using field_t = std::array<line_t, height>;
using point_t = std::array<int, 2>;
using oxygens_t = std::vector<point_t>;
constexpr int64_t directions[] = {4, 1, 3, 2}; // E, N, W, S
constexpr point_t vectors[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
const D2D1::ColorF brush_colors[] = {
{0.000f, 0.000f, 0.000f, 1.000f}, // 0: black
{1.000f, 0.000f, 0.000f, 1.000f}, // 1: red
{0.000f, 1.000f, 0.000f, 1.000f}, // 2: green
{0.227f, 0.455f, 1.000f, 1.000f}, // 3: blue
{0.000f, 1.000f, 1.000f, 1.000f}, // 4: cyan
{1.000f, 0.000f, 1.000f, 1.000f}, // 5: magenta
{1.000f, 1.000f, 0.000f, 1.000f}, // 6: yellow
{1.000f, 1.000f, 1.000f, 1.000f}, // 7: white
{0.200f, 0.000f, 0.000f, 1.000f}, // 8: dark red
{0.600f, 0.420f, 0.220f, 1.000f}, // 9: light brown
{0.380f, 0.275f, 0.122f, 1.000f}, // 10: dark brown
{0.400f, 0.400f, 0.400f, 1.000f}, // 11: grey
};
namespace colors {
constexpr int unknown = 0;
constexpr int wall = 10;
constexpr int home = 2;
constexpr int oxygen = 3;
constexpr int new_oxygen = 4;
constexpr int droid = 6;
constexpr int pump = 4;
constexpr int path = 9;
constexpr int empty = 9;
constexpr int snake = 6;
} // namespace colors
std::ostream &operator<<(std::ostream &stream, const point_t &point) {
auto [x, y] = point;
return stream << "(" << x << "," << y << ")";
}
struct BeginDrawGuard {
BeginDrawGuard(ID2D1RenderTarget *r) : r(r) { r->BeginDraw(); }
~BeginDrawGuard() noexcept(false) { CHECK_HRESULT(r->EndDraw()); }
ID2D1RenderTarget *r;
};
void do_it(program_t program) {
program.resize(65536);
int64_t pc = 0, base = 0;
std::vector<int64_t> inputs;
int64_t output;
field_t maze;
int direction = 0;
const point_t home_pos = {starti, startj};
point_t pos = home_pos;
point_t snake[snake_length];
point_t pump_pos = {-1, -1};
int time = 0;
int distance = 0;
int pump_distance = 0;
for (auto &snake_pos : snake) {
snake_pos = pos;
}
d2d_t d2d;
d2d_thread_t d2d_thread(d2d, width * scale, height * scale + 30);
auto RenderTarget = d2d_thread.GetRenderTarget();
ID2D1SolidColorBrushPtr Brushes[std::size(brush_colors)];
int frame_index = 0;
for (std::size_t i = 0; i != std::size(brush_colors); ++i) {
Brushes[i] = d2d_thread.CreateSolidBrush(brush_colors[i]);
}
auto paint_oxygens = [&](const oxygens_t &oxygens, int color_index) {
for (const auto [i, j] : oxygens) {
float x = j * scalef;
float y = i * scalef;
D2D1_RECT_F rect = {x, y, x + scalef, y + scalef};
RenderTarget->FillRectangle(&rect, Brushes[color_index]);
}
};
auto paint_point = [&](const point_t &point, int color_index) {
auto [i, j] = point;
float x = j * scalef;
float y = i * scalef;
D2D1_RECT_F rect = {x, y, x + scalef, y + scalef};
RenderTarget->FillRectangle(&rect, Brushes[color_index]);
};
auto paint_wall = [&](const point_t &point, int direction, int color_index) {
auto [i, j] = point;
float x = j * scalef;
float y = i * scalef;
D2D1_RECT_F rect;
switch (direction) {
case 0:
// right
rect = {x + scale, y, x + scale + line_width, y + scale + line_width};
break;
case 1:
// down
rect = {x - line_width, y + scale, x + scale, y + scale + line_width};
break;
case 2:
// left
rect = {x - line_width, y - line_width, x, y + scale};
break;
case 3:
// up
rect = {x, y - line_width, x + scale + line_width, y};
break;
}
RenderTarget->FillRectangle(&rect, Brushes[color_index]);
};
auto emit_frame = [&](int distance) {
{
BeginDrawGuard guard(RenderTarget);
auto caption = std::to_wstring(distance);
std::basic_string<WCHAR> all_on(std::size(caption), L'8');
D2D1_RECT_F rect = {
1.0f, height * scalef, 121.0f, height * scalef + 30.0f};
RenderTarget->FillRectangle(&rect, Brushes[0]);
d2d_thread.draw_text(all_on.c_str(), 1.0f, height * scalef + 1.0f,
Brushes[8], text_style::segment, text_anchor::topleft);
d2d_thread.draw_text(caption.c_str(), 1.0f, height * scalef + 1.0f,
Brushes[1], text_style::segment, text_anchor::topleft);
}
std::basic_ostringstream<WCHAR> ostr;
ostr << L".obj/frame-" << std::setfill(L'0') << std::setw(4)
<< frame_index++ << L".png";
auto filename = ostr.str();
d2d_thread.write_png(filename.c_str());
};
std::memset(&maze, '\0', sizeof maze);
maze[pos[0]][pos[1]] = {empty, 0};
{
BeginDrawGuard guard(RenderTarget);
D2D1_RECT_F rect = {0, 0, width * scale, height * scale};
RenderTarget->Clear({0.0f, 0.0f, 0.0f, 1.0f});
RenderTarget->FillRectangle(&rect, Brushes[colors::unknown]);
paint_point(pos, colors::droid);
}
emit_frame(pump_distance ? pump_distance : distance);
// Explore the maze for an arbitrary number of turns.
// Keep the droid's right hand on a wall.
while (true) {
point_t newpos = pos;
newpos[0] += vectors[direction][0];
newpos[1] += vectors[direction][1];
if (maze[newpos[0]][newpos[1]].contents == wall) {
// There is a wall ahead. Turn right.
{
BeginDrawGuard guard(RenderTarget);
paint_wall(pos, direction, colors::wall);
}
direction = (direction + 1) & 3;
// Quit after we return to the origin.
if (pos == home_pos && direction == 0) {
break;
}
continue;
}
// Try to go forward.
output = run_until_output(
program, pc, base, [&]() { return directions[direction]; }, false);
if (output == 0) {
// We hit a wall. Update map and turn right.
{
BeginDrawGuard guard(RenderTarget);
paint_wall(pos, direction, colors::wall);
}
maze[newpos[0]][newpos[1]].contents = wall;
direction = (direction + 1) & 3;
continue;
}
// We went forward.
pos = newpos;
// Emit a frame.
{
BeginDrawGuard guard(RenderTarget);
paint_point(snake[0], colors::empty);
for (int i = 1; i != snake_length; ++i) {
snake[i - 1] = snake[i];
paint_point(snake[i], colors::snake);
}
snake[snake_length - 1] = pos;
paint_point(home_pos, colors::home);
paint_point(pos, colors::droid);
}
if (time++ % explore_speed == 0) {
emit_frame(pump_distance ? pump_distance : distance);
}
{
BeginDrawGuard guard(RenderTarget);
paint_point(pos, colors::empty);
paint_point(home_pos, colors::home);
paint_point(pump_pos, colors::pump);
}
// Update map.
++distance;
if (tile_t tile = maze[pos[0]][pos[1]]; tile.contents == unknown) {
if (output == 2) {
// We found the pump.
pump_distance = distance;
pump_pos = pos;
maze[pos[0]][pos[1]] = tile_t{pump, distance};
} else {
maze[pos[0]][pos[1]] = tile_t{empty, distance};
}
} else {
distance = std::min(distance, tile.distance);
}
// Turn left.
direction = (direction + 3) & 3;
}
{
BeginDrawGuard guard(RenderTarget);
for (int i = 0; i != snake_length; ++i) {
paint_point(snake[i], colors::empty);
}
paint_point(home_pos, colors::home);
paint_point(pos, colors::droid);
}
// Fill with oxygen.
oxygens_t oxygens[oxygen_length + 1];
auto & new_oxygens = oxygens[oxygen_length];
new_oxygens.push_back(pump_pos);
time = 0;
bool finished = false;
while (true) {
maze[pump_pos[0]][pump_pos[1]] = {pump, 0};
{
BeginDrawGuard guard(RenderTarget);
paint_oxygens(oxygens[0], colors::oxygen);
paint_oxygens(new_oxygens, colors::new_oxygen);
paint_point(home_pos, colors::home);
paint_point(pump_pos, colors::pump);
}
if (time % oxygen_speed == 0) {
emit_frame(time);
}
if (finished) {
break;
}
std::rotate(&oxygens[0], &oxygens[1], &oxygens[oxygen_length + 1]);
new_oxygens.resize(0);
++time;
finished = true;
for (auto point : oxygens[oxygen_length - 1]) {
for (int direction = 0; direction != 4; ++direction) {
int i = point[0] + vectors[direction][0];
int j = point[1] + vectors[direction][1];
if (auto tile = maze[i][j];
tile.contents == empty || tile.contents == home) {
maze[i][j] = {tile.contents == home ? home : oxygen, time};
new_oxygens.push_back({i, j});
finished = false;
}
}
}
}
for (const auto & oxygen: oxygens) {
BeginDrawGuard guard(RenderTarget);
paint_oxygens(oxygen, colors::oxygen);
paint_point(home_pos, colors::home);
paint_point(pump_pos, colors::pump);
}
int oxygenation_time = time - 1;
std::cout << "Part one, distance " << maze[starti][startj].distance << "\n"
<< "Part two, time " << oxygenation_time << std::endl;
// Just for fun, mark the shortest path.
pos = home_pos;
direction = 0;
distance = maze[starti][startj].distance;
maze[starti][startj] = {droid, -1};
while (distance) {
// Find an adjacent square one step closer to the pump.
point_t newpos = {
pos[0] + vectors[direction][0], pos[1] + vectors[direction][1]};
auto tile = maze[newpos[0]][newpos[1]];
if ((tile.contents == oxygen || tile.contents == pump) &&
tile.distance == distance - 1) {
pos = newpos;
--distance;
maze[pos[0]][pos[1]] = {path, -1};
} else {
direction = (direction + 1) & 3;
continue;
}
{
BeginDrawGuard guard(RenderTarget);
paint_point(snake[0], colors::empty);
for (int i = 1; i != snake_length; ++i) {
snake[i - 1] = snake[i];
paint_point(snake[i], colors::snake);
}
snake[snake_length - 1] = pos;
paint_point(pos, colors::droid);
paint_point(home_pos, colors::home);
paint_point(pump_pos, colors::pump);
}
if (time++ % explore_speed == 0) {
emit_frame(oxygenation_time);
}
{
BeginDrawGuard guard(RenderTarget);
paint_point(pos, colors::path);
paint_point(home_pos, colors::home);
paint_point(pump_pos, colors::pump);
}
}
{
BeginDrawGuard guard(RenderTarget);
for (const auto & point: snake) {
paint_point(point, colors::empty);
}
paint_point(home_pos, colors::home);
paint_point(pos, colors::droid);
}
emit_frame(oxygenation_time);
}
int CALLBACK _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) {
try {
auto programs = read_programs("15.data");
do_it(programs[0]);
return 0;
} catch (const char *e) {
std::cout << e << std::endl;
return 1;
}
}