-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.cpp
338 lines (276 loc) · 8.94 KB
/
day17.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
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <memory>
#include <ostream>
#include <queue>
#include <set>
#include <string>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>
using Location = std::complex<int16_t>;
using Direction = std::complex<int16_t>;
struct LocationUtil final {
bool operator()(const Location &l1, const Location &l2) const noexcept {
if (l1.real() != l2.real())
return l1.real() < l2.real();
if (l1.imag() != l2.imag())
return l1.imag() < l2.imag();
return false;
}
size_t operator()(const Location &l1) const noexcept {
return l1.real() * 37 + l1.imag() * 23;
}
};
class Map final {
private:
std::unique_ptr<const uint8_t[]> data;
size_t width;
Map(std::unique_ptr<const uint8_t[]> &&data, size_t width)
: data(std::move(data)), width(width) {}
public:
uint8_t at(const Location &point) const {
return data.get()[point.real() * width + point.imag()] - '0';
}
uint8_t size() const noexcept { return width; }
void printMap(std::ostream &out = std::cout) const {
for (size_t i = 0; i < width; ++i) {
for (size_t j = 0; j < width; ++j) {
out << at(Location(i, j));
}
out << "\n";
}
}
static Map fromFile(const std::string &filename) {
auto input = std::ifstream(filename);
std::string buff;
std::getline(input, buff);
input >> std::ws;
size_t width = buff.size();
auto data = std::make_unique<uint8_t[]>(width * width);
std::copy(buff.begin(), buff.end(), data.get());
for (size_t i = 1; i < width; ++i) {
input.read(reinterpret_cast<char *>(data.get() + i * width), width);
input >> std::ws;
}
return Map(std::move(data), width);
}
};
struct Position final {
uint16_t dist;
uint16_t steps;
Direction dir;
Location loc;
bool operator<(const Position &other) const noexcept {
if (dist != other.dist)
return dist < other.dist;
if (steps != other.steps)
return steps < other.steps;
// required only for backtracking to work properly
// if (loc != other.loc)
// return LocationUtil{}(loc, other.loc);
// if (dir != other.dir)
// return LocationUtil{}(dir, other.dir);
return false;
}
bool operator>(const Position &other) const noexcept {
if (dist != other.dist)
return dist > other.dist;
if (steps != other.steps)
return steps > other.steps;
// required only for backtracking to work properly
// if (loc != other.loc)
// return LocationUtil{}(other.loc, loc);
// if (dir != other.dir)
// return LocationUtil{}(other.dir, dir);
return false;
}
};
std::ostream &operator<<(std::ostream &out, const Position &pos) {
return out << "{" << pos.dist << "," << pos.steps << ",pos:" << pos.loc
<< ",dir:" << pos.dir << "}";
}
template <uint32_t minStep, uint32_t maxStep, bool doBacktracking = false>
class Dijkstra final {
const Map ↦
const size_t width;
const size_t area = width * width;
std::vector<bool> data;
std::vector<uint16_t> bestDistances;
std::vector<Position> backtrack;
public:
Dijkstra(const Map &map)
: map(map), width(map.size()),
data(std::vector<bool>(4 * maxStep * area)),
bestDistances(std::vector<uint16_t>(4 * maxStep * area)),
backtrack(
std::vector<Position>(doBacktracking ? 4 * maxStep * area : 0)) {}
private:
size_t hash(const Position &pos) const noexcept {
uint8_t dirReal = static_cast<uint8_t>(pos.dir.real());
uint8_t dirImag = static_cast<uint8_t>(pos.dir.imag());
const size_t hashDir = ((dirReal | dirImag) & 0b10) | (dirImag & 0b01);
const size_t hash = hashDir * maxStep * area + pos.steps * area +
pos.loc.imag() * width + pos.loc.real();
return hash;
}
void visit(const Position &pos) {
// data[hash(pos)] = true;
// 70% speed increase
for (uint16_t i = pos.steps; i < maxStep; ++i) {
data[hash({pos.dist, i, pos.dir, pos.loc})] = true;
}
}
bool at(const Position &pos) const { return data[hash(pos)]; }
// 20% speed increase (could be more if I removed the default bit set)
bool proposeDistance(const Position &pos) {
const auto h = hash(pos);
const auto soFarTheBest = bestDistances[h];
if (soFarTheBest == 0 || pos.dist < soFarTheBest) {
bestDistances[h] = pos.dist;
return true;
} else {
return false;
}
}
std::priority_queue<Position, std::vector<Position>, std::greater<Position>>
queue;
void insertStart(Position &&pos) {
if constexpr (doBacktracking) {
backtrack[hash(pos)] = pos;
}
insert<minStep>(pos, pos.dir, minStep - 1);
}
void insertBacktracking(const Position &prev, const Position &now) {
if constexpr (!doBacktracking)
return;
auto &orginal = backtrack[hash(now)];
if (orginal.dir == Direction(0, 0) || now < orginal) {
backtrack[hash(now)] = prev;
}
}
template <uint16_t moveScale>
void insert(const Position &pos, const Direction &dir,
const uint16_t newSteps) {
const Location newLoc = pos.loc + dir * static_cast<int16_t>(moveScale);
// I can't think of a good use for padding here
if (newSteps >= maxStep || static_cast<uint16_t>(newLoc.real()) >= width ||
static_cast<uint16_t>(newLoc.imag()) >= width) {
return;
}
uint16_t newDist = pos.dist;
for (size_t i = 1; i <= moveScale; ++i) {
const uint16_t odlDist = newDist;
newDist += map.at(pos.loc + dir * static_cast<int16_t>(i));
if constexpr (doBacktracking) {
const auto prev =
i == 1 ? pos
: Position{
odlDist,
static_cast<uint16_t>(newSteps - (moveScale - i) - 1),
dir, pos.loc + dir * static_cast<int16_t>(i - 1)};
const auto now =
Position{newDist, static_cast<uint16_t>(newSteps - (moveScale - i)),
dir, pos.loc + dir * static_cast<int16_t>(i)};
insertBacktracking(prev, now);
}
}
const Position newPos = {newDist, newSteps, dir, newLoc};
// just a little optimization (30% speed up)
if (at(newPos)) {
return;
}
if (proposeDistance(newPos)) {
queue.emplace(std::move(newPos));
}
}
void process(const Position &&pos) {
insert<1>(pos, pos.dir, pos.steps + 1);
insert<minStep>(pos, pos.dir * Direction(0, 1), minStep - 1);
insert<minStep>(pos, pos.dir * Direction(0, -1), minStep - 1);
}
std::unordered_set<Location, LocationUtil> backtrackPos(Position pos) {
// std::set<Location, LocationUtil> backtrackResult;
std::unordered_set<Location, LocationUtil> backtrackResult;
// std::cout << "Backtracking" << std::endl;
while (true) {
// std::cout << pos << std::endl;
backtrackResult.insert(pos.loc);
if (pos.loc == Location(0, 0)) {
break;
}
pos = backtrack[hash(pos)];
}
return backtrackResult;
}
Position finalPosition;
public:
uint16_t run(const Location pos) {
insertStart({0, 0, Direction(1, 0), Location(0, 0)});
insertStart({0, 0, Direction(0, 1), Location(0, 0)});
const Location target = Location(width - 1, width - 1);
while (!queue.empty()) {
const auto item = std::move(queue.top());
queue.pop();
if (at(item)) {
continue;
}
if (item.loc == target) {
finalPosition = item;
return item.dist;
}
visit(item);
process(std::move(item));
}
return -1;
}
void showPath(std::ostream &out = std::cout) {
if constexpr (!doBacktracking) {
return;
}
const auto backtrackResult = backtrackPos(finalPosition);
for (size_t i = 0; i < width; ++i) {
for (size_t j = 0; j < width; ++j) {
const auto loc = Location(i, j);
out << (char)(backtrackResult.count(loc) ? 'O' : '~');
}
out << "\n";
}
}
};
template <uint32_t minStep, uint32_t maxStep, bool doBacktracking = false>
size_t part0X(const std::string &filename) {
const auto map = Map::fromFile(filename);
auto dijikstra = Dijkstra<minStep, maxStep, doBacktracking>(map);
const auto res = dijikstra.run(Location(0, 0));
dijikstra.showPath();
return res;
}
size_t part01(const std::string &filename) {
return part0X<1, 3, true>(filename);
}
size_t part02(const std::string &filename) {
return part0X<4, 10, true>(filename);
}
int main() {
std::string fileTest = "input_test.txt";
std::string fileProd = "input_prod.txt";
assert(part01(fileTest) == 102);
const auto part01Res = part01(fileProd);
std::cout << "Part 01: " << part01Res << std::endl;
assert(part01Res == 959);
assert(part02(fileTest) == 94);
const auto part02Res = part02(fileProd);
std::cout << "Part 02: " << part02Res << std::endl;
assert(part02Res == 1135);
}