-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_12b.cpp
65 lines (63 loc) · 2.07 KB
/
day_12b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <regex>
#include <cmath>
#include <queue>
void add_pipe_to_map(const std::string& pipe, std::unordered_map<int, std::unordered_set<int>>& map) {
std::size_t start = 0;
const std::string delimiter = ", ";
std::size_t end = pipe.find(' ');
const int from_program = std::stoi(pipe.substr(start, end - start));
start = end + 1;
start = pipe.find(' ', start) + 1;
end = pipe.find(delimiter, start);
while(end != std::string::npos) {
const int to_program = std::stoi(pipe.substr(start, end - start));
start = end + delimiter.size();
end = pipe.find(delimiter, start);
map[from_program].insert(to_program);
map[to_program].insert(from_program);
}
const int to_program = std::stoi(pipe.substr(start, end - start));
map[from_program].insert(to_program);
map[to_program].insert(from_program);
}
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_12_input" ;
std::ifstream file(input);
std::string line;
std::unordered_map<int, std::unordered_set<int>> map;
while(std::getline(file, line)) {
add_pipe_to_map(line, map);
}
std::unordered_set<int> reachable_prime;
int n_groups = 0;
for (const auto& [start_program, reachables] : map) {
if (reachable_prime.find(start_program) != reachable_prime.end()) continue;
n_groups++;
std::queue<int> to_expand;
to_expand.push(start_program);
// Using a new smaller set for efficiency
std::unordered_set<int> reachable;
while(!to_expand.empty()) {
const auto& new_reachables = map[to_expand.front()];
to_expand.pop();
for (const auto& new_reachable : new_reachables) {
if (reachable.find(new_reachable) != reachable.end()) {
continue;
}
to_expand.push(new_reachable);
reachable.insert(new_reachable);
reachable_prime.insert(new_reachable);
}
}
}
std::cout << n_groups << '\n';
return 0;
}