-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_17b.cpp
165 lines (147 loc) · 4.73 KB
/
day_17b.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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_set>
#include <vector>
// TODO: add a lookup for seen points that were not expanded
struct Point {
int row;
int col;
Point(const int row, const int col) : row(row), col(col) {}
Point operator + (const Point& p) const {
return Point (p.row + row, p.col + col);
}
bool operator == (const Point& p) const {
return p.row == row && p.col == col;
}
};
struct State {
Point position = Point(0,0);
int direction = 0;
int moves_in_dir = 0;
std::size_t heat_loss = 0;
bool operator == (const State& state) const {
return state.position == position && state.moves_in_dir == moves_in_dir && state.direction == direction;
// heat loss should not need a comparison as using a priority queue
}
friend std::ostream& operator << (std::ostream& os, const State& s);
};
std::ostream& operator << (std::ostream& os, const State& s) {
os << "(" << s.position.row << "," << s.position.col << ") " << s.direction << ' ' << s.moves_in_dir << ' ' << s.heat_loss;
return os;
}
struct Comparator {
bool operator() (const State& s1, const State& s2) const {
return s1.heat_loss > s2.heat_loss;
}
};
struct hasher {
std::size_t operator() (const State& state) const {
return state.position.row + state.position.col;
}
};
const std::vector<Point> motions ={
Point(-1,0),
Point(0,1),
Point(1,0),
Point(0, -1),
};
bool in_limits(const std::vector<std::vector<int>>& map, const int row, const int col) {
return row >= 0 && row < map.size() && col >= 0 && col < map[0].size();
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_17_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<int>> map;
while(std::getline(file, line)) {
map.emplace_back();
for (const auto c : line) {
map.back().emplace_back(c - '0');
}
}
std::priority_queue<State, std::vector<State>, Comparator> pq;
std::unordered_set<State, hasher> seen;
// Enfore the 4 step minimum at the starting point by forcing a turn at the start
// So start facing N or W, then turn and move.
State state;
state.position = Point(0,0);
state.direction = 3;
state.moves_in_dir = 0;
state.heat_loss = 0;
pq.push(state);
state.direction = 0;
pq.push(state);
const auto destination = Point(map.size() - 1, map[0].size() -1);
while(!pq.empty()) {
state = pq.top();
pq.pop();
if (seen.find(state) != seen.end()) {
continue;
}
seen.insert(state);
if (state.position == destination) {
std::cout << state.heat_loss << '\n';
return 0;
}
// Move left
{
State new_state;
new_state.position = state.position;
new_state.direction = (state.direction - 1 + 4) % 4;
new_state.heat_loss = state.heat_loss;
new_state.moves_in_dir = 0;
bool moved_four_steps = true;
for (int step_idx = 0; step_idx < 4; step_idx++) {
new_state.position = new_state.position + motions[new_state.direction];
moved_four_steps = in_limits(map, new_state.position.row, new_state.position.col);
if (!moved_four_steps) break;
new_state.heat_loss = new_state.heat_loss + std::size_t(map[new_state.position.row][new_state.position.col]);
new_state.moves_in_dir++;
}
if (moved_four_steps) {
pq.push(new_state);
}
}
// Move right
{
State new_state;
new_state.position = state.position;
new_state.direction = (state.direction + 1 + 4) % 4;
new_state.heat_loss = state.heat_loss;
bool moved_four_steps = true;
new_state.moves_in_dir = 0;
for (int step_idx = 0; step_idx < 4; step_idx++) {
new_state.position = new_state.position + motions[new_state.direction];
moved_four_steps = in_limits(map, new_state.position.row, new_state.position.col);
if (!moved_four_steps) break;
new_state.heat_loss = new_state.heat_loss + std::size_t(map[new_state.position.row][new_state.position.col]);
new_state.moves_in_dir++;
}
if (moved_four_steps) {
pq.push(new_state);
}
}
// Move Straight
{
if (state.moves_in_dir <= 9) {
State new_state = state;
new_state.position = state.position + motions[state.direction];
if (in_limits(map, new_state.position.row, new_state.position.col)) {
new_state.heat_loss = state.heat_loss + std::size_t(map[new_state.position.row][new_state.position.col]);
new_state.moves_in_dir++;
pq.push(new_state);
}
}
}
}
std::cout << "The destination was not reached." << '\n';
return 0;
}