-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge23.cpp
311 lines (259 loc) · 12.1 KB
/
challenge23.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
#include "challenge23.hpp"
#include "helper.hpp"
#include "print.hpp"
#include <algorithm>
#include <memory>
#include <ranges>
#include <unordered_map>
namespace {
enum class Direction : std::int8_t { Up, Left, Down, Right };
Direction turnRight(Direction d) noexcept {
return static_cast<Direction>((static_cast<int>(d) + 4 - 1) % 4);
}
Direction turnLeft(Direction d) noexcept {
return static_cast<Direction>((static_cast<int>(d) + 1) % 4);
}
Direction turnAround(Direction d) noexcept {
return static_cast<Direction>((static_cast<int>(d) + 2) % 4);
}
bool isUpDown(Direction d) noexcept {
using enum Direction;
return d == Up || d == Down;
}
auto directionRange(void) noexcept {
return std::views::iota(0, 4) | std::views::transform([](int i) noexcept { return static_cast<Direction>(i); });
}
auto directionRangeWithout(Direction blocked) noexcept {
return std::views::iota(0, 4) | std::views::transform([](int i) noexcept { return static_cast<Direction>(i); }) |
std::views::filter([blocked](Direction d) noexcept { return d != blocked; });
}
template<typename T>
struct Coordinate {
T Row;
T Column;
constexpr bool operator==(const Coordinate&) const noexcept = default;
constexpr auto operator<=>(const Coordinate&) const noexcept = default;
Coordinate left(void) const noexcept {
return {Row, Column - 1};
}
Coordinate right(void) const noexcept {
return {Row, Column + 1};
}
Coordinate up(void) const noexcept {
return {Row - 1, Column};
}
Coordinate down(void) const noexcept {
return {Row + 1, Column};
}
Coordinate& move(Direction direction, T distance = 1) noexcept {
switch ( direction ) {
using enum Direction;
case Left : Column -= distance; break;
case Up : Row -= distance; break;
case Right : Column += distance; break;
case Down : Row += distance; break;
} //switch ( direction )
return *this;
}
Coordinate moved(Direction direction, T distance = 1) const noexcept {
auto ret = *this;
ret.move(direction, distance);
return ret;
}
};
} //namespace
namespace std {
template<typename T>
struct hash<Coordinate<T>> {
size_t operator()(const Coordinate<T>& c) const noexcept {
constexpr auto bits = std::numeric_limits<T>::digits / 2;
constexpr auto mask = ((T{1} << bits) - 1);
return std::hash<T>{}((c.Row << bits) | (c.Column & mask));
}
};
} //namespace std
namespace {
struct Node {
std::array<Node*, 2> Successors{nullptr, nullptr};
std::array<Node*, 2> Predecessors{nullptr, nullptr};
std::int32_t LongestPathFromTarget{0};
void addSuccessor(Node* successor) {
if ( !Successors[0] ) {
Successors[0] = successor;
return;
} //if ( !Successors[0] )
throwIfInvalid(Successors[0] != successor);
throwIfInvalid(!Successors[1]);
Successors[1] = successor;
return;
}
};
struct Crossing;
struct Pathway {
std::int32_t Length = 1;
Crossing* Begin = nullptr;
Crossing* End = nullptr;
};
struct Crossing {
std::array<Pathway*, 4> Pathways{};
std::int32_t LongestPathFromTarget = 0;
};
struct Graph {
using Coordinate = ::Coordinate<std::size_t>;
Pathway* StartPathway;
Pathway* TargetPathway;
std::vector<std::unique_ptr<Pathway>> Pathways;
std::unordered_map<Coordinate, Pathway*> PathwayMap;
std::unordered_map<Coordinate, std::unique_ptr<Crossing>> Crossings;
std::size_t MaxRow;
std::size_t MaxColumn;
Graph(const std::vector<std::string_view>& input) :
StartPathway{nullptr}, TargetPathway{nullptr}, MaxRow{input.size()},
MaxColumn{(throwIfInvalid(MaxRow > 2), input.front().size())} {
const auto firstLine = input.front();
const auto lastLine = input.back();
const Coordinate startCoordinate{0, firstLine.find('.')};
const Coordinate targetCoordinate{MaxRow - 1, lastLine.find('.')};
throwIfInvalid(startCoordinate.Column != std::string_view::npos);
throwIfInvalid(targetCoordinate.Column != std::string_view::npos);
StartPathway =
buildPathway<true>(input, startCoordinate, Direction::Up, /*fromCrossing=*/nullptr, targetCoordinate);
throwIfInvalid(TargetPathway);
//Das erste Feld ist kein Schritt.
--StartPathway->Length;
return;
}
template<bool PartOne>
std::int32_t calculateLongestPath(void) noexcept {
std::vector<Crossing*> visited;
return calculateLongestPath<PartOne>(TargetPathway->Begin, TargetPathway, visited);
}
template<bool PartOne>
std::int32_t calculateLongestPath(Crossing* currentCrossing, Pathway* incomingPathway,
std::vector<Crossing*>& visited) noexcept {
auto iter = std::ranges::lower_bound(visited, currentCrossing);
visited.insert(iter, currentCrossing);
const auto blockedDirection = directionOfPathway(incomingPathway, currentCrossing);
const auto isUpOrLeft = [](Direction direction) noexcept {
return direction == Direction::Up || direction == Direction::Left;
};
const auto filter = [&isUpOrLeft](Direction direction) noexcept {
if constexpr ( PartOne ) {
return isUpOrLeft(direction);
} //if constexpr ( PartOne )
static_cast<void>(isUpOrLeft);
return true;
};
std::int32_t max = 0;
for ( auto direction : directionRangeWithout(blockedDirection) | std::views::filter(filter) ) {
auto pathway = currentCrossing->Pathways[static_cast<std::size_t>(direction)];
if ( !pathway ) {
continue;
} //if ( !pathway )
Crossing* nextCrossing = isUpOrLeft(direction) ? pathway->Begin : pathway->End;
if ( !nextCrossing ) {
max = pathway->Length;
break;
} //if ( !nextCrossing )
if ( std::ranges::binary_search(visited, nextCrossing) ) {
continue;
} //if ( std::ranges::binary_search(visited, nextCrossing) )
max = std::max(max, calculateLongestPath<PartOne>(nextCrossing, pathway, visited));
} //for ( auto direction : directionRangeWithout(blockedDirection) | std::views::filter(filter) )
iter = std::ranges::lower_bound(visited, currentCrossing);
throwIfInvalid(*iter == currentCrossing);
visited.erase(iter);
return max + 1 + incomingPathway->Length;
}
private:
bool isValid(Coordinate c) const noexcept {
return c.Row < MaxRow && c.Column < MaxColumn;
}
template<bool AssignStart>
Pathway* buildPathway(const std::vector<std::string_view>& input, Coordinate pathwayStart,
Direction blockedDirection, Crossing* fromCrossing, Coordinate targetCoordinate) {
auto field = input[pathwayStart.Row][pathwayStart.Column];
if ( field == '#' ) {
return nullptr;
} //if ( field == '#' )
if ( auto iter = PathwayMap.find(pathwayStart); iter != PathwayMap.end() ) {
return iter->second;
} //if ( auto iter = PathwayMap.find(pathwayStart); iter != PathwayMap.end() )
auto pathway = Pathways.emplace_back(std::make_unique<Pathway>()).get();
const bool topDown = blockedDirection == Direction::Up || blockedDirection == Direction::Left;
if constexpr ( AssignStart ) {
StartPathway = pathway;
} //if constexpr ( AssignStart )
else {
(topDown ? pathway->Begin : pathway->End) = fromCrossing;
} //else -> if constexpr ( AssignStart )
PathwayMap.emplace(pathwayStart, pathway);
do { //while ( field == '.' )
Direction neighborDirection = *(directionRangeWithout(blockedDirection) |
std::views::drop_while([&input, &pathwayStart](Direction d) noexcept {
auto p = pathwayStart.moved(d);
return input[p.Row][p.Column] == '#';
})).begin();
++pathway->Length;
pathwayStart.move(neighborDirection);
blockedDirection = turnAround(neighborDirection);
field = input[pathwayStart.Row][pathwayStart.Column];
if ( pathwayStart == targetCoordinate ) {
PathwayMap.emplace(pathwayStart, pathway);
TargetPathway = pathway;
return TargetPathway;
} //if ( pathwayStart == targetCoordinate )
} while ( field == '.' );
switch ( field ) {
case '>' :
case 'v' : {
Direction toCrossing = fieldToDirection(field);
if ( !topDown ) {
toCrossing = turnAround(toCrossing);
} //if ( !topDown )
PathwayMap.emplace(pathwayStart, pathway);
(topDown ? pathway->End : pathway->Begin) =
buildCrossing(input, pathwayStart.moved(toCrossing), toCrossing, pathway, targetCoordinate);
break;
} //case '>' & 'v'
default : throwIfInvalid(false);
} //switch ( field )
return pathway;
}
Crossing* buildCrossing(const std::vector<std::string_view>& input, const Coordinate crossingCoordinate,
Direction intoCrossing, Pathway* firstPathway, const Coordinate& targetCoordinate) {
auto fromCrossing = turnAround(intoCrossing);
if ( auto iter = Crossings.find(crossingCoordinate); iter != Crossings.end() ) {
auto crossing = iter->second.get();
crossing->Pathways[static_cast<std::size_t>(fromCrossing)] = firstPathway;
return crossing;
} //if ( auto iter = Crossings.find(crossingCoordinate); iter != Crossings.end() )
auto crossing = Crossings.emplace(crossingCoordinate, std::make_unique<Crossing>()).first->second.get();
crossing->Pathways[static_cast<std::size_t>(fromCrossing)] = firstPathway;
for ( auto direction : directionRangeWithout(fromCrossing) ) {
crossing->Pathways[static_cast<std::size_t>(direction)] = buildPathway<false>(
input, crossingCoordinate.moved(direction), turnAround(direction), crossing, targetCoordinate);
} //for ( auto direction : directionRangeWithout(fromCrossing) )
return crossing;
}
static Direction fieldToDirection(char field) noexcept {
switch ( field ) {
case '>' : return Direction::Right;
case 'v' : return Direction::Down;
default : throwIfInvalid(false); return Direction::Up;
} //switch ( field )
}
static Direction directionOfPathway(Pathway* pathway, Crossing* crossing) noexcept {
return static_cast<Direction>(
std::ranges::distance(crossing->Pathways.begin(), std::ranges::find(crossing->Pathways, pathway)));
}
};
} //namespace
bool challenge23(const std::vector<std::string_view>& input) {
Graph graph{input};
auto maxDirectedPathLength = graph.calculateLongestPath<true>();
myPrint(" == Result of Part 1: {:d} ==\n", maxDirectedPathLength);
auto maxUndirectedPathLength = graph.calculateLongestPath<false>();
myPrint(" == Result of Part 2: {:d} ==\n", maxUndirectedPathLength);
return maxDirectedPathLength == 2106 && maxUndirectedPathLength == 6350;
}