-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18a.cpp
148 lines (135 loc) · 4.54 KB
/
day_18a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <vector>
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;
}
};
long long BFS(const std::vector<std::vector<char>>& map, const Point& start, std::map<char, std::vector<Point>>& reachables) {
// 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();
q.pop();
std::bitset<n_keys> path = cur.path;
if (path.all()) {
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]) && !(path[map[new_pt.y][new_pt.x] - 'A'])) 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;
}
}
}
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::map<char, std::vector<Point>> reachables;
reachables.insert({'@', Dijkstra(map, start)});
for (const auto [c, pc] : keys_and_doors) {
reachables.insert({c, Dijkstra(map, pc)});
}
const long long min_steps = BFS(map, start, reachables);
std::cout << min_steps << '\n';
return min_steps;
}