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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;
using graph_type = map<string, set<string>>;
vector<string>
split(const string& line, char sep)
{
vector<string> parts;
stringstream input{ line };
for ( string part; getline(input, part, sep); ) {
parts.emplace_back(part);
}
return parts;
}
graph_type
read_file(string_view filename)
{
fstream input{ filename };
graph_type graph;
for ( string line; getline(input, line); ) {
const auto components = split(line.erase(line.find(':'), 1), ' ');
for ( size_t i = 1; i < components.size(); ++i ) {
graph[components[0]].insert(components[i]);
graph[components[i]].insert(components[0]);
}
}
return graph;
}
bool
bfs(const graph_type& graph, const string& source, const string& dest, map<string, string>& parents) // NOLINT
{
set<string> visited;
parents.clear();
queue<string> queue;
queue.emplace(source);
while ( !queue.empty() ) {
auto candidate = queue.front();
queue.pop();
if ( candidate == dest ) {
return true;
}
for ( const auto& neighbour: graph.at(candidate) ) {
if ( !visited.contains(neighbour) ) {
queue.emplace(neighbour);
parents[neighbour] = candidate;
visited.emplace(neighbour);
}
}
}
return false;
}
void
dfs(const graph_type& graph, const string& source, set<string>& visited) // NOLINT
{
visited.emplace(source);
for ( const auto& node: graph.at(source) ) {
if ( !visited.contains(node) ) {
dfs(graph, node, visited);
}
}
}
bool
bfs1(graph_type graph, const string& source, const string& dest, function<void(const string&, const string&)> visit) // NOLINT
{
set<string> visited;
queue<string> queue;
queue.emplace(source);
while ( !queue.empty() ) {
auto candidate = queue.front();
queue.pop();
if ( candidate == dest ) {
return true;
}
for ( const auto& neighbour: graph[candidate] ) {
if ( !visited.contains(neighbour) ) {
queue.emplace(neighbour);
visit(candidate, neighbour);
visited.emplace(neighbour);
}
}
}
return false;
}
void
print_graph_stats(const graph_type& graph)
{
size_t nodes = 0;
size_t edges = 0;
for ( const auto& link: graph ) {
nodes++;
edges += link.second.size();
}
cout << nodes << ", " << edges / 2 << endl;
}
void
part1(graph_type graph)
{
srand(unsigned(time(nullptr)));
vector<string> nodes(graph.size());
for ( const auto& foo: graph ) {
nodes.emplace_back(foo.first);
}
map<tuple<string, string>, int> counter;
for ( size_t idx = 0; idx != 2; ++idx ) {
for ( const auto& second: graph ) {
bfs1(graph, nodes[size_t(rand()) % nodes.size()], second.first, [&](const string& candidate, const string& neighbour) {
if ( candidate < neighbour ) {
counter[make_tuple(candidate, neighbour)]++;
}
else {
counter[make_tuple(neighbour, candidate)]++;
}
});
}
}
vector<tuple<tuple<string, string>, int>> all_links{ counter.begin(), counter.end() };
sort(all_links.begin(), all_links.end(), [](const auto& lhs, const auto& rhs) {
return get<1>(lhs) > get<1>(rhs);
});
for ( size_t i = 0; i != 3; ++i ) {
const auto [links, num] = all_links[i];
const auto [lhs, rhs] = links;
graph[lhs].erase(rhs);
graph[rhs].erase(lhs);
}
set<string> reachable;
const auto start_from_a = get<0>(get<0>(all_links[0]));
bfs1(graph, start_from_a, "--not there--", [&](const string& candidate, const string& neighbour) {
reachable.insert(candidate);
reachable.insert(neighbour);
});
set<string> reachable2;
const auto start_from_b = get<1>(get<0>(all_links[0]));
bfs1(graph, start_from_b, "--not there--", [&](const string& candidate, const string& neighbour) {
reachable2.insert(candidate);
reachable2.insert(neighbour);
});
cout << reachable.size() * reachable2.size() << endl;
}
void
print_graph(const graph_type& graph)
{
for ( const auto& node: graph ) {
cout << node.first << ": ";
copy(node.second.begin(), node.second.end(), ostream_iterator<string>(cout, ", "));
cout << endl;
}
}
graph_type
get_rgraph(const graph_type& graph)
{
graph_type rgraph;
for ( const auto& node: graph ) {
for ( const auto& link: node.second ) {
rgraph[link].insert(node.first);
}
}
return rgraph;
}
int
main()
{
auto graph = read_file("data/day25.txt");
// print_graph(graph);
part1(graph);
// print_graph(graph);
// cout << " ------------ " << endl;
// print_graph(get_rgraph(graph));
// cout << " ------------ " << endl;
// print_graph(get_rgraph(get_rgraph(graph)));
// map<string, string> parents;
// if ( bfs(graph, "jqt", "bvb", parents) ) {
// for ( const auto& [lhs, rhs]: parents ) {
// cout << lhs << ": " << rhs << endl;
// }
// }
}
|