-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge16.cpp
More file actions
151 lines (119 loc) · 5.85 KB
/
challenge16.cpp
File metadata and controls
151 lines (119 loc) · 5.85 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
#include "challenge16.hpp"
#include "helper.hpp"
#include "print.hpp"
#include <algorithm>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace {
using Position = Coordinate<std::int64_t>;
struct State {
Position Pos;
std::int64_t Cost;
Direction CurrentDirection;
std::int64_t HeuristicValue;
std::pair<Position, Direction> Predecessor;
State(void) noexcept = default;
State(std::pair<Position, Direction> posAndDirection) noexcept :
Pos{posAndDirection.first}, Cost{0}, CurrentDirection{posAndDirection.second} {
return;
}
bool operator==(const State& that) const noexcept {
return Pos == that.Pos && CurrentDirection == that.CurrentDirection;
}
};
struct StateHash {
static std::size_t operator()(const State& state) noexcept {
return std::hash<Position>{}(state.Pos) + static_cast<std::size_t>(state.CurrentDirection);
}
};
struct CostData {
std::int64_t Cost;
std::vector<std::pair<Position, Direction>> Precedessors;
};
auto findCheapestPath(MapView map) {
const Position start{Position::MaxRow - 2, 1};
const Position end{1, Position::MaxColumn - 2};
throwIfInvalid(map[start] == 'S');
throwIfInvalid(map[end] == 'E');
std::vector<State> toVisit;
std::unordered_map<State, CostData, StateHash> leastCost;
auto push = [map, &toVisit, end](State state, State previousState) noexcept {
if ( map[state.Pos] == '#' ) {
return;
} //if ( map[state.Pos] == '#' )
state.HeuristicValue = state.Cost + (end.Column - state.Pos.Column + state.Pos.Row - end.Row);
state.Predecessor = {previousState.Pos, previousState.CurrentDirection};
toVisit.push_back(state);
std::ranges::push_heap(toVisit, std::ranges::greater{}, &State::HeuristicValue);
return;
};
State initial{{start, Direction::Right}};
push(initial, initial);
std::int64_t cheapestCost = std::numeric_limits<std::int64_t>::max();
while ( !toVisit.empty() ) {
std::ranges::pop_heap(toVisit, std::ranges::greater{}, &State::HeuristicValue);
const auto currentState = toVisit.back();
toVisit.pop_back();
if ( auto costIter = leastCost.find(currentState); costIter != leastCost.end() ) {
if ( currentState.Cost > costIter->second.Cost ) {
//++skipped;
continue;
} //if ( currentState.Cost > costIter->second.Cost )
if ( currentState.Cost == costIter->second.Cost ) {
costIter->second.Precedessors.push_back(currentState.Predecessor);
continue;
} //if ( currentState.Cost == costIter->second.Cost )
costIter->second = {currentState.Cost, {}};
} //if ( auto costIter = leastCost.find(currentState); costIter != leastCost.end() )
else {
if ( currentState.Pos != end || currentState.Cost <= cheapestCost ) {
leastCost.emplace(currentState, CostData{currentState.Cost, std::vector{currentState.Predecessor}});
} //if ( currentState.Pos != end || currentState.Cost <= cheapestCost )
} //else -> if ( auto costIter = leastCost.find(currentState); costIter != leastCost.end() )
if ( currentState.Pos == end ) {
cheapestCost = std::min(cheapestCost, currentState.Cost);
continue;
} //if ( currentState.Pos == end )
State movedState;
movedState.CurrentDirection = currentState.CurrentDirection;
movedState.Pos = currentState.Pos.moved(movedState.CurrentDirection);
movedState.Cost = currentState.Cost + 1;
push(movedState, currentState);
State turnedLeftState;
turnedLeftState.CurrentDirection = turnLeft(currentState.CurrentDirection);
turnedLeftState.Pos = currentState.Pos.moved(turnedLeftState.CurrentDirection);
turnedLeftState.Cost = currentState.Cost + 1001;
push(turnedLeftState, currentState);
State turnedRightState;
turnedRightState.CurrentDirection = turnRight(currentState.CurrentDirection);
turnedRightState.Pos = currentState.Pos.moved(turnedRightState.CurrentDirection);
turnedRightState.Cost = currentState.Cost + 1001;
push(turnedRightState, currentState);
} //while ( !toVisit.empty() )
std::unordered_set<Position> visited{start};
auto addVisited = [&visited, &leastCost, &start](this auto& self,
std::pair<Position, Direction> posAndDirection) noexcept -> void {
if ( posAndDirection.first == start ) {
return;
} //if ( posAndDirection.first == start )
visited.insert(posAndDirection.first);
for ( auto precedessor : leastCost[State{posAndDirection}].Precedessors ) {
self(precedessor);
} //for ( auto precedessor : leastCost[State{posAndDirection}].Precedessors )
return;
};
addVisited({end, Direction::Up});
addVisited({end, Direction::Right});
return std::pair{cheapestCost, visited.size()};
}
} //namespace
bool challenge16(const std::vector<std::string_view>& input) {
Position::setMaxFromMap(input);
const auto [pathCost, nodesOnPathes] = findCheapestPath(input);
myPrint(" == Result of Part 1: {:d} ==\n", pathCost);
// const auto nodesOnPathes = findAllNodesOnCheapestPaths(input, pathCost);
myPrint(" == Result of Part 2: {:d} ==\n", nodesOnPathes);
return pathCost == 105496 && nodesOnPathes == 524;
}