-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_06a.cpp
60 lines (56 loc) · 1.88 KB
/
day_06a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
void recursiveOrbitCount(
const std::string& current_object,
const std::unordered_map<std::string, std::unordered_set<std::string>>&
is_orbitted_by,
std::unordered_map<std::string, int>& n_orbits) {
if (is_orbitted_by.find(current_object) == std::end(is_orbitted_by)) {
n_orbits[current_object] = 0;
} else {
n_orbits[current_object] = 0;
const auto ele_it = is_orbitted_by.find(current_object);
for (const auto orbitting_object : ele_it->second) {
recursiveOrbitCount(orbitting_object, is_orbitted_by, n_orbits);
n_orbits[current_object] += n_orbits[orbitting_object] + 1;
}
}
}
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_06_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::unordered_map<std::string, std::string> orbits;
std::unordered_map<std::string, std::unordered_set<std::string>>
is_orbitted_by;
std::unordered_map<std::string, int> n_orbits;
// Solve
std::string line;
const std::string delim = ")";
while (std::getline(file, line)) {
const size_t delim_pos = line.find(delim);
const std::string primary_object = line.substr(0, delim_pos);
const std::string orbitting_object = line.substr(
delim_pos + delim.size(), line.size() - delim_pos - delim.size());
orbits[orbitting_object] = primary_object;
if (is_orbitted_by.find(primary_object) == std::end(is_orbitted_by)) {
is_orbitted_by.insert({primary_object, {}});
}
is_orbitted_by[primary_object].insert(orbitting_object);
}
recursiveOrbitCount("COM", is_orbitted_by, n_orbits);
size_t total = 0;
for (const auto& [key, val] : n_orbits) {
total += val;
}
std::cout << total << '\n';
return total;
}