-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_12a.cpp
64 lines (58 loc) · 1.48 KB
/
day_12a.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
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
void print(const std::vector<std::string>& v) {
for (const auto& s : v) {
std::cout << s << ' ';
}
std::cout << '\n';
}
void recurse (const std::string& s,
std::unordered_set<std::string>& seen,
const std::unordered_map<std::string, std::vector<std::string>>& map,
long long& count) {
if (s == "end") {
count++;
return;
}
if (s[0] > 97 && s[0] <=122) {
seen.insert(s);
}
const auto it = map.find(s);
for (const auto& next : it->second) {
if (seen.find(next) == seen.end()) {
recurse(next, seen, map, count);
}
}
seen.erase(s);
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_12_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::unordered_map<std::string, std::vector<std::string>> map;
while(std::getline(file, line)) {
const auto d = line.find('-');
const auto p1 = line.substr(0, d);
const auto p2 = line.substr(d + 1, line.size() - d - 1);
if (map.find(p1) == map.end()) {
map[p1] = std::vector<std::string>();
}
if (map.find(p2) == map.end()) {
map[p2] = std::vector<std::string>();
}
map[p1].emplace_back(p2);
map[p2].emplace_back(p1);
}
long long count = 0;
std::unordered_set<std::string> seen;
recurse("start", seen, map, count);
std::cout << count << '\n';
return 0;
}