-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18b.cpp
228 lines (209 loc) · 7.06 KB
/
day_18b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <vector>
// Assumed that the doors with keys outside the quadrant can be ignored. Works for all but the last example.
// TODO(vss): Modify the BFS to track the state of all 4 robots and their previously visited paths (ie exapnd to n bots)
constexpr int n_keys = 26;
struct Point {
Point (const int x = 0, const int y = 0, const int dist = 0, const long long path = 0, const std::vector<char>& order = {}) : x(x), y(y), dist(dist), path(path), order(order) {}
Point (const Point&) = default;
Point (Point&&) = default;
Point& operator=(const Point&) = default;
Point& operator=(Point&&) = default;
bool operator==(const Point& p) {
return p.x == x && p.y ==y;
}
int x;
int y;
int dist;
long long path;
std::vector<char> order;
};
std::vector<Point> getMoves() {
std::vector<Point> moves;
moves.emplace_back(+1, 0);
moves.emplace_back(-1, 0);
moves.emplace_back(0, +1);
moves.emplace_back(0, -1);
return moves;
}
bool inBounds(const Point& current, const std::vector<std::vector<char>>& map) {
return current.y >=0 && current.y < map.size() && current.x >=0 && current.x <= map[0].size();
}
std::vector<Point> Dijkstra(const std::vector<std::vector<char>>& map, const Point& start) {
std::queue<Point> points;
std::set<std::tuple<int, int>> visited;
std::vector<Point> reachable;
points.push(start);
while (!points.empty()) {
const auto current = points.front();
points.pop();
visited.insert({current.x, current.y});
for (const auto& move : getMoves()) {
const Point neighbour(current.x + move.x, current.y + move.y, current.dist + 1);
if (!inBounds(neighbour, map)) continue;
if (map[neighbour.y][neighbour.x] == '#') continue;
if (visited.find({neighbour.x, neighbour.y}) != visited.end()) continue;
if (map[neighbour.y][neighbour.x] == '.') {
points.push(neighbour);
} else if (map[neighbour.y][neighbour.x] != '#') {
if (std::find(reachable.begin(), reachable.end(), neighbour) == reachable.end()) {
reachable.push_back(neighbour);
}
}
}
}
return reachable;
}
struct CompareDist {
bool operator()(const Point& p1, const Point& p2) {
return p1.dist > p2.dist;
}
};
bool ShouldIgnore(const char c, const int quad, std::map<char, int> kd_quads) {
if (kd_quads[tolower(c)] != quad) return true;
return false;
}
long long BFS(
const std::vector<std::vector<char>>& map,
const Point& start, std::map<char,
std::vector<Point>>& reachables,
const int quad,
const int n_keys_in_quad,
std::map<char, int>& kd_quads) {
// State of point = x, y, and order of previously visited points, ie path
std::priority_queue<Point,std::vector<Point>, CompareDist> q;
std::set<std::tuple<int, int, long long>> visited; // Need unique state
q.push(start);
visited.insert({start.x, start.y, start.path});
int count = 0;
while(!q.empty()) {
const auto cur = q.top();
// std::cout << "popped: " << map[cur.y][cur.x] << '\n';
q.pop();
std::bitset<n_keys> path = cur.path;
// std::cout << path << '\n';
// std::cout << n_keys_in_quad << '\n';
if (path.count() == n_keys_in_quad) {
std::cout << "Returning" << '\n';
std::cout << cur.dist << '\n';
return cur.dist;
}
for(const auto& reachable_point : reachables[map[cur.y][cur.x]]) {
auto temp = cur.order;
temp.emplace_back(map[cur.y][cur.x]);
Point new_pt(reachable_point.x, reachable_point.y, cur.dist + reachable_point.dist, cur.path, temp);
if (visited.find({new_pt.x, new_pt.y, new_pt.path}) != visited.end()) continue;
if (isupper(map[new_pt.y][new_pt.x]) && !ShouldIgnore(map[new_pt.y][new_pt.x], quad, kd_quads) && !(path[map[new_pt.y][new_pt.x] - 'A'])) {
std::cout << "Continue called for " << map[new_pt.y][new_pt.x] << '\n';
continue;
}
bool changed = false;
if (islower(map[new_pt.y][new_pt.x]) && !path[map[new_pt.y][new_pt.x] - 'a']) {
changed = true;
path[map[new_pt.y][new_pt.x] - 'a'] = 1;
}
visited.insert({new_pt.x, new_pt.y, new_pt.path});
new_pt.path = path.to_ullong();
q.push(new_pt);
if (changed) {
path[map[new_pt.y][new_pt.x] - 'a'] = 0;
}
}
}
std::cout << "Error " << '\n';
return 0;
}
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_18_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string input_line;
std::vector<std::vector<char>> map;
std::map<char, Point> keys_and_doors;
Point start;
while(std::getline(file, input_line)) {
map.emplace_back(input_line.begin(), input_line.end());
for (int i = 0; i < input_line.size(); i++) {
if (input_line[i] == '@') {
start.x = i;
start.y = map.size() - 1;
} else if (input_line[i] != '.' && input_line[i] != '#') {
keys_and_doors.insert({input_line[i], Point(i, map.size() - 1)});
}
}
}
// Solve
std::vector<Point> starts{
Point(start.x-1, start.y-1),
Point(start.x-1, start.y+1),
Point(start.x+1, start.y+1),
Point(start.x+1, start.y-1)
};
std::map<char, int> kd_quads;
std::set<char> tbd;
std::vector<int> key_counts(4, 0);
for (const auto& [kd, coord] : keys_and_doors) {
if (coord.x < start.x && coord.y < start.y) {
kd_quads[kd] = 0;
if (islower(kd)) key_counts[0]++;
} else if (coord.x < start.x && coord.y > start.y) {
kd_quads[kd] = 1;
if (islower(kd)) key_counts[1]++;
} else if (coord.x > start.x && coord.y > start.y) {
kd_quads[kd] = 2;
if (islower(kd)) key_counts[2]++;
} else if (coord.x > start.x && coord.y < start.y) {
kd_quads[kd] = 3;
if (islower(kd)) key_counts[3]++;
} else {
tbd.insert(kd);
}
}
map[start.y + 1][start.x + 1] = '@';
map[start.y + 1][start.x - 1] = '@';
map[start.y - 1][start.x + 1] = '@';
map[start.y - 1][start.x - 1] = '@';
map[start.y][start.x + 1] = '#';
map[start.y + 1][start.x] = '#';
map[start.y][start.x - 1] = '#';
map[start.y - 1][start.x] = '#';
map[start.y][start.x] = '#';
std::map<char, std::vector<Point>> reachables;
for (const auto [c, pc] : keys_and_doors) {
reachables.insert({c, Dijkstra(map, pc)});
}
// TODO(vss): Change to recursipon to allow for edge cases
for (const char c : tbd) {
bool found = false;
while (!found) {
for (const auto& p : reachables[c]) {
if (kd_quads.find(map[p.y][p.x]) != kd_quads.end()) {
found = true;
kd_quads[c] = kd_quads[map[p.y][p.x]];
if (islower(c)) key_counts[kd_quads[c]]++;
break;
}
}
}
}
long long min_steps = 0;
for (int i = 0; i < 4; i++) {
reachables.erase('@');
reachables.insert({'@', Dijkstra(map, starts[i])});
min_steps += BFS(map, starts[i], reachables, i, key_counts[i], kd_quads);
}
std::cout << min_steps << '\n';
return min_steps;
}