-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_24b.cpp
201 lines (178 loc) · 7.02 KB
/
day_24b.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
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
struct Storm {
Storm(const int period, const int offset) : period(period), offset(offset) {}
int period;
int offset;
friend std::ostream& operator<<(std::ostream& os, const Storm& p);
};
std::ostream& operator<<(std::ostream& os, const Storm& p) {
os << '(' << p.period << ' ' << p.offset << ')';
return os;
}
struct State {
State (const int row = 0, const int col = 0, const int time = 0) : row(row), col(col), time(time) {}
int row;
int col;
int time;
bool operator == (const State& p) const {
return p.row == row && p.col == col && p.time == time;
}
friend std::ostream& operator<<(std::ostream& os, const State& p);
};
struct CompareStateTime {
bool operator () (const State& s1, const State& s2) const {
return s1.time < s2.time;
}
};
struct StateHash {
std::size_t operator () (const State& p) const {
return (p.row * 10) + p.col;
}
};
std::ostream& operator<<(std::ostream& os, const State& p) {
os << '(' << p.row << ' ' << p.col << ' ' << p.time << ')';
return os;
}
State find_path(const State& start, const State& end, const std::vector<std::vector<std::vector<Storm>>>& time_offsets, const std::vector<std::string>& grid) {
const int n_rows = time_offsets.size();
const int n_cols = time_offsets[0].size();
const std::array<State, 5> moves = {{
State( 0, 1),
State( 1, 0),
State( 0, -1),
State(-1, 0),
State( 0, 0)
}};
const auto in_bounds = [&n_rows, &n_cols, &grid](const State& s) {
return s.row >= 0 && s.col >=0 && s.row < n_rows && s.col < n_cols && grid[s.row][s.col] != '#';
};
const auto is_storm = [&time_offsets](const State& s) {
for (const auto storm : time_offsets[s.row][s.col]) {
if (s.time % storm.period == storm.offset) {
return true;
}
}
return false;
};
std::queue<State> q;
q.push(start);
std::unordered_set<State, StateHash> visited;
while(!q.empty()) {
const auto current = q.front();
q.pop();
if (visited.find(current) != visited.end()) continue;
if (current.time > (n_cols - 2) * (n_rows - 2)) {
auto temp = current;
temp.time = temp.time % ((n_cols - 2) * (n_rows - 2));
if (visited.find(temp) != visited.end()) continue;
}
visited.insert(current);
if (current.row == end.row && current.col == end.col) {
std::cout << current.time << '\n';
return current;
}
for (const auto move : moves) {
const auto new_state = State (current.row + move.row, current.col + move.col, current.time + 1);
// std::cout << "New State: " << new_state << " ---> ";
if (!in_bounds(new_state)) {
// std::cout << "out of bounds" << '\n';
continue;
}
if (is_storm(new_state)) {
// std::cout << "is a storm" << '\n';
continue;
}
// std::cout << " is acceptable" << '\n';
q.push(new_state);
}
}
std::cout << "Could not find path" << '\n';
return State();
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_24_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::string> grid;
while(std::getline(file, line)) {
// std::cout << '|' << line << '|' << '\n';
grid.push_back(line);
}
const int n_rows = grid.size();
const int n_cols = grid[0].size();
// std::cout << n_rows << ' ' << n_cols << '\n';
std::vector<std::vector<std::vector<Storm>>> time_offsets(n_rows, std::vector<std::vector<Storm>>(n_cols));
for (int row = 1; row < n_rows-1; row++) {
for (int col = 1; col < n_cols-1; col++) {
// std::cout << row << ' ' << col << '\n';
if (grid[row][col] == '^') {
// std::cout << grid[row][col] << '\n';
const auto period = n_rows - 2;
// std::cout << period << '\n';
int offset = 0;
for (int i = row; i > 0; i--, offset++) {
time_offsets[i][col].emplace_back(Storm(period, offset));
}
for (int i = n_rows-1-1; i > row; i--, offset++) {
time_offsets[i][col].emplace_back(Storm(period, offset));
}
}
else if (grid[row][col] == 'v') {
// std::cout << grid[row][col] << '\n';
const auto period = n_rows - 2;
// std::cout << period << '\n';
int offset = 0;
for (int i = row; i < (n_rows-1); i++, offset++) {
time_offsets[i][col].emplace_back(Storm(period, offset));
}
for (int i = 1; i < row; i++, offset++) {
time_offsets[i][col].emplace_back(Storm(period, offset));
}
}
else if (grid[row][col] == '>') {
// std::cout << grid[row][col] << '\n';
const auto period = n_cols - 2;
// std::cout << period << '\n';
int offset = 0;
for (int i = col; i < (n_cols-1); i++, offset++) {
time_offsets[row][i].emplace_back(Storm(period, offset));
}
for (int i = 1; i < col; i++, offset++) {
time_offsets[row][i].emplace_back(Storm(period, offset));
}
}
else if (grid[row][col] == '<') {
// std::cout << grid[row][col] << '\n';
const auto period = n_cols - 2;
// std::cout << period << '\n';
int offset = 0;
for (int i = col; i > 0; i--, offset++) {
time_offsets[row][i].emplace_back(Storm(period, offset));
}
for (int i = n_cols-1-1; i > col; i--, offset++) {
time_offsets[row][i].emplace_back(Storm(period, offset));
}
}
}
}
const auto state_at_goal = find_path(State(0, 1, 0), State(n_rows-1, n_cols-2, 0), time_offsets, grid);
std::cout << state_at_goal << '\n';
const auto state_back_at_start = find_path(state_at_goal, State(0, 1, 0), time_offsets, grid);
std::cout << state_back_at_start << '\n';
const auto state_back_at_goal = find_path(state_back_at_start, State(n_rows-1, n_cols-2, 0), time_offsets, grid);
std::cout << state_back_at_goal << '\n';
std::cout << state_back_at_goal.time << '\n';
return 0;
}