-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6-part2.cpp
54 lines (40 loc) · 1.08 KB
/
day6-part2.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
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
int main() {
const std::string start = "YOU";
const std::string end = "SAN";
std::unordered_map<std::string, std::unordered_set<std::string>> graph;
while (true) {
std::string line;
std::getline(std::cin, line);
size_t parenIndex = line.find(')');
if (std::cin.eof()) {
break;
}
std::string parent = line.substr(0, parenIndex);
std::string child = line.substr(parenIndex + 1);
graph[parent].insert(child);
graph[child].insert(parent);
}
std::unordered_map<std::string, size_t> distances;
distances.insert({start, -2});
std::queue<std::string> toVisit;
toVisit.push(start);
while(!distances.count(end)) {
std::string vertex = toVisit.front();
toVisit.pop();
size_t distance = distances[vertex];
for (auto &neighbor : graph[vertex]) {
if (distances.count(neighbor)) {
continue;
}
distances.insert({neighbor, distance + 1});
toVisit.push(neighbor);
}
}
std::cout << distances[end] << std::endl;
return 0;
}