-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17.cpp
More file actions
363 lines (317 loc) · 12.4 KB
/
day17.cpp
File metadata and controls
363 lines (317 loc) · 12.4 KB
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
#include "day17.h"
#include "helpers.h"
#include <array>
#include <cassert>
#include <fstream>
#include <iostream>
#include <ranges>
#include <queue>
#include <string>
#include <vector>
namespace day17
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 17 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day17_example.txt" : "inputs/day17_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file) << '\n';
file.close();
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
enum class Direction
{
north,
east,
south,
west
};
struct State
{
size_t x{};
size_t y{};
int nrMovedStraight{};
Direction lastDirection{};
int costSoFar{};
size_t heuristicScore{};
[[nodiscard]] auto calculateHeuristicScore(const size_t goalX, const size_t goalY) const
{
return costSoFar + (goalX - x) + (goalY - y);
}
State(size_t xP, size_t yP, int nrMovedStraightP, Direction lastDirectionP, int costSoFarP, size_t goalX, size_t goalY)
{
x = xP;
y = yP;
nrMovedStraight = nrMovedStraightP;
lastDirection = lastDirectionP;
costSoFar = costSoFarP;
heuristicScore = calculateHeuristicScore(goalX, goalY);
}
};
struct City
{
std::vector<std::vector<int>> map;
size_t width;
size_t height;
explicit City(std::ifstream& file)
{
map = std::vector<std::vector<int>>{};
while(!file.eof())
{
std::string line;
std::getline(file, line);
std::vector<int> row{};
for (char c : line)
{
row.push_back(c - '0');
}
map.push_back(row);
}
width = map[0].size();
height = map.size();
}
[[nodiscard]] auto heuristicScoreState(const State& s) const
{
return (width - s.x - 1) + (height - s.y - 1);
}
};
// A*, though state is complicated by the 3 in 1 direction restraint.
// Using manhattan distance as remainder heuristic
long long determineShortestPathLength(const City& city)
{
const size_t goalX{ city.width - 1 };
const size_t goalY{ city.height - 1 };
const State startPos{ 0, 0, 0, Direction::east, 0, goalX, goalY };
// To keep track of passed states
// bool, 16 array for nrMovedStraight * 4 + direction.
std::vector passed(city.height, std::vector(city.width, std::array<bool, 16>{}));
auto cmp = [](const State l, const State r) { return l.heuristicScore > r.heuristicScore; };
std::priority_queue<State, std::vector<State>, decltype(cmp)> statePriorityQueue(cmp);
statePriorityQueue.push(startPos);
while(!statePriorityQueue.empty())
{
State s{ statePriorityQueue.top() };
statePriorityQueue.pop();
const auto directionIndex = s.nrMovedStraight * 4LL + (static_cast<size_t>(s.lastDirection));
if (passed[s.y][s.x][directionIndex])
{
continue;
}
passed[s.y][s.x][directionIndex] = true;
if (s.y == goalY && s.x == goalX)
{
return s.costSoFar;
}
if (s.y > 0 && s.lastDirection != Direction::south)
{
if (s.lastDirection == Direction::north)
{
if (s.nrMovedStraight < 3)
{
State newState{ s.x, s.y - 1, s.nrMovedStraight + 1, Direction::north, s.costSoFar + city.map[s.y - 1][s.x], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
State newState{ s.x, s.y - 1, 1, Direction::north, s.costSoFar + city.map[s.y - 1][s.x], goalX, goalY };
statePriorityQueue.push(newState);
}
}
if (s.y < goalY && s.lastDirection != Direction::north)
{
if (s.lastDirection == Direction::south)
{
if (s.nrMovedStraight < 3)
{
State newState{ s.x, s.y + 1, s.nrMovedStraight + 1, Direction::south, s.costSoFar + city.map[s.y + 1][s.x], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
State newState{ s.x, s.y + 1, 1, Direction::south, s.costSoFar + city.map[s.y + 1][s.x], goalX, goalY };
statePriorityQueue.push(newState);
}
}
if (s.x > 0 && s.lastDirection != Direction::east)
{
if (s.lastDirection == Direction::west)
{
if (s.nrMovedStraight < 3)
{
State newState{ s.x - 1, s.y, s.nrMovedStraight + 1, Direction::west, s.costSoFar + city.map[s.y][s.x - 1], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
State newState{ s.x - 1, s.y, 1, Direction::west, s.costSoFar + city.map[s.y][s.x - 1], goalX, goalY };
statePriorityQueue.push(newState);
}
}
if (s.x < goalX && s.lastDirection != Direction::west)
{
if (s.lastDirection == Direction::east)
{
if (s.nrMovedStraight < 3)
{
State newState{ s.x + 1, s.y, s.nrMovedStraight + 1, Direction::east, s.costSoFar + city.map[s.y][s.x + 1], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
State newState{ s.x + 1, s.y, 1, Direction::east, s.costSoFar + city.map[s.y][s.x + 1], goalX, goalY };
statePriorityQueue.push(newState);
}
}
}
// Found no path to goal
assert(false);
return 0;
}
// A*, though state is complicated by the min 4, max 10 in 1 direction restraint.
// Using manhattan distance as remainder heuristic
// Not actually 100% sure that heuristic is admissible here, because of the >= 4 moved straight
// requirement at the end, but it works for the inputs.
long long determineShortestPathLengthForUltraCrucible(const City& city)
{
const size_t goalX{ city.width - 1 };
const size_t goalY{ city.height - 1 };
const State startPos{ 0, 0, 0, Direction::east, 0, goalX, goalY };
// To keep track of passed states
// bool, 44 array for nrMovedStraight * 4 + direction.
std::vector passed(city.height, std::vector(city.width, std::array<bool, 44>{}));
auto cmp = [](const State l, const State r) { return l.heuristicScore > r.heuristicScore; };
std::priority_queue<State, std::vector<State>, decltype(cmp)> statePriorityQueue(cmp);
statePriorityQueue.push(startPos);
while (!statePriorityQueue.empty())
{
State s{ statePriorityQueue.top() };
statePriorityQueue.pop();
const auto directionIndex = s.nrMovedStraight * 4LL + (static_cast<size_t>(s.lastDirection));
if (passed[s.y][s.x][directionIndex])
{
continue;
}
passed[s.y][s.x][directionIndex] = true;
if (s.y == goalY && s.x == goalX && s.nrMovedStraight >= 4)
{
return s.costSoFar;
}
if (s.y > 0 && s.lastDirection != Direction::south)
{
if (s.lastDirection == Direction::north)
{
if (s.nrMovedStraight < 10)
{
State newState{ s.x, s.y - 1, s.nrMovedStraight + 1, Direction::north, s.costSoFar + city.map[s.y - 1][s.x], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
if (s.nrMovedStraight >= 4 && s.y > 3)
{
int costSoFar{ s.costSoFar };
for(int i =1; i <= 4; i++)
{
costSoFar += city.map[s.y - i][s.x];
}
State newState{ s.x, s.y - 4, 4, Direction::north, costSoFar, goalX, goalY };
statePriorityQueue.push(newState);
}
}
}
if (s.y < goalY && s.lastDirection != Direction::north)
{
if (s.lastDirection == Direction::south)
{
if (s.nrMovedStraight < 10)
{
State newState{ s.x, s.y + 1, s.nrMovedStraight + 1, Direction::south, s.costSoFar + city.map[s.y + 1][s.x], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
if (s.nrMovedStraight >= 4 && s.y < goalY - 3)
{
int costSoFar{ s.costSoFar };
for (int i = 1; i <= 4; i++)
{
costSoFar += city.map[s.y + i][s.x];
}
State newState{ s.x, s.y + 4, 4, Direction::south, costSoFar, goalX, goalY };
statePriorityQueue.push(newState);
}
}
}
if (s.x > 0 && s.lastDirection != Direction::east)
{
if (s.lastDirection == Direction::west)
{
if (s.nrMovedStraight < 10)
{
State newState{ s.x - 1, s.y, s.nrMovedStraight + 1, Direction::west, s.costSoFar + city.map[s.y][s.x - 1], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
if (s.nrMovedStraight >= 4 && s.x > 3)
{
int costSoFar{ s.costSoFar };
for (int i = 1; i <= 4; i++)
{
costSoFar += city.map[s.y][s.x - i];
}
State newState{ s.x - 4, s.y, 4, Direction::west, costSoFar, goalX, goalY };
statePriorityQueue.push(newState);
}
}
}
if (s.x < goalX && s.lastDirection != Direction::west)
{
if (s.lastDirection == Direction::east)
{
if (s.nrMovedStraight < 10)
{
State newState{ s.x + 1, s.y, s.nrMovedStraight + 1, Direction::east, s.costSoFar + city.map[s.y][s.x + 1], goalX, goalY };
statePriorityQueue.push(newState);
}
}
else
{
if (s.nrMovedStraight >= 4 && s.x < goalX - 3)
{
int costSoFar{ s.costSoFar };
for (int i = 1; i <= 4; i++)
{
costSoFar += city.map[s.y][s.x + i];
}
State newState{ s.x + 4, s.y, 4, Direction::east, costSoFar, goalX, goalY };
statePriorityQueue.push(newState);
}
}
}
}
// Found no path to goal
assert(false);
return 0;
}
long long solvePart1(std::ifstream& file)
{
const City city(file);
return determineShortestPathLength(city);
}
long long solvePart2(std::ifstream& file)
{
const City city(file);
return determineShortestPathLengthForUltraCrucible(city);
}
}