-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
289 lines (221 loc) · 9.25 KB
/
main.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream>
#include <cstdint>
#include <memory>
#include <vector>
#include <fstream>
#include <map>
#include <algorithm>
#include <iterator>
#undef ENABLE_DEBUG
// Clang __unused-compat polyfill
#ifndef __unused
#define __unused
#endif
#ifdef ENABLE_DEBUG
#define DEBUG_STREAM std::cout
#else
std::ofstream null_stream("/dev/null");
#define DEBUG_STREAM null_stream
#endif
namespace thomas {
class shop {
private:
uint64_t identifier;
std::vector<shop *> connected_shops;
public:
explicit shop(const uint64_t identifier) : identifier(identifier) {
// Empty implementation
}
inline std::vector<shop *> &get_connected_shops() noexcept {
return connected_shops;
}
inline const uint64_t get_identifier() const noexcept {
return identifier;
}
};
class network {
private:
struct _impact_entry {
uint64_t impact;
};
std::map<uint64_t, thomas::shop *> shops;
inline std::map<uint64_t, thomas::shop *> &get_shops() noexcept {
return shops;
}
public:
explicit network() {
shops = std::map<uint64_t, thomas::shop *>();
}
virtual ~network() {
for (auto &each_pair : shops) {
delete each_pair.second;
}
shops.clear();
}
inline shop *shop_at(const uint64_t i) {
return get_shops().at(i);
}
inline void register_shop(shop *shop) {
shops.insert(std::make_pair(shop->get_identifier(), shop));
}
inline uint64_t number_of_connections() noexcept {
uint64_t counter = 0;
for (auto &each_pair : get_shops()) {
shop *shop = each_pair.second;
for (auto &remote_shop : shop->get_connected_shops()) {
(void)remote_shop;
counter += 1;
}
}
return counter;
}
/**
reduce()
Reduces the network one step down by disposing nodes to two subtle parts.
@return The number of nodes disposed.
*/
inline uint64_t reduce() noexcept {
std::vector<shop *> linear_shops;
std::map<uint64_t, shop *> *shops = &get_shops();
linear_shops.reserve(shops->size());
std::for_each(shops->begin(),
shops->end(),
[&linear_shops] (const std::map<uint64_t, shop *>::value_type& pair) {
linear_shops.push_back(pair.second);
});
// Sort the linear container with number of connections
std::sort(linear_shops.begin(),
linear_shops.end(),
[] (shop *lhs, shop *rhs) {
return lhs->get_connected_shops().size() > rhs->get_connected_shops().size();
});
const uint64_t threshold = linear_shops.front()->get_connected_shops().size();
DEBUG_STREAM << "reducing with threshold value of " << threshold << std::endl;
// Filter out the elements with connection size lower than threshold value
linear_shops.erase(std::remove_if(linear_shops.begin(),
linear_shops.end(),
[&threshold] (shop *el) {
return el->get_connected_shops().size() < threshold;
}), linear_shops.end());
std::vector<_impact_entry> impacts;
impacts.reserve(linear_shops.size());
// Find the external impact of each node
std::for_each(linear_shops.begin(),
linear_shops.end(),
[&impacts,
&linear_shops] (shop *el) {
uint64_t impact = 0;
for (auto &each_connection : el->get_connected_shops()) {
// Check whether the connection is external
if (std::find(linear_shops.begin(), linear_shops.end(), each_connection) == linear_shops.end()) {
impact += each_connection->get_connected_shops().size();
}
}
impacts.push_back({
impact
});
});
// Sort the array with impact values descending
std::sort(impacts.begin(),
impacts.end(),
[] (_impact_entry lhs,
_impact_entry rhs) {
return lhs.impact > rhs.impact;
});
const uint64_t required_impact = impacts.front().impact;
// Filter out the elements with impact lower than required
impacts.erase(std::remove_if(impacts.begin(),
impacts.end(),
[&required_impact /* clang-capture-default */] (_impact_entry el) {
return el.impact < required_impact;
}), impacts.end());
// If the number of elements remaining is lower than two, no further action is required
if (impacts.size() < 2) {
return 0;
}
return impacts.size();
}
friend std::ostream &operator<<(std::ostream &os, network &network);
};
std::ostream &operator<< (std::ostream &os, network &network) {
for (auto &each_pair : network.get_shops()) {
shop *shop = each_pair.second;
for (auto &remote_shop : shop->get_connected_shops()) {
os << shop->get_identifier() << " is connected with " << remote_shop->get_identifier() << std::endl;
}
}
return os;
}
}
int32_t main(int32_t argc, const char * argv[]) {
if (argc != 2) {
std::cerr << "argument error: missing file argument" << std::endl;
std::terminate();
}
std::ifstream input_file(argv[1]);
if (!input_file.is_open()) {
std::cerr << "io error: file couldn't be opened" << std::endl;
std::terminate();
}
std::string buffer_str;
uint64_t num_shops, num_roads;
// Fetch the number of shops and roads
std::getline(input_file, buffer_str);
if (std::sscanf(buffer_str.data(), "%lu %lu", &num_shops, &num_roads) != 2) {
std::cerr << "parsing error: couldn't parse header" << std::endl;
std::terminate();
}
if (num_shops < 2 || num_shops > 1000) {
std::cerr << "argument error: number of shops should be in between 2 to 1000 inclusive" << std::endl;
std::terminate();
}
if (num_roads < 1 || num_roads > 1000) {
std::cerr << "argument error: number of roads should be in between 1 to 1000 inclusive" << std::endl;
std::terminate();
}
thomas::network network;
for (uint64_t i = 0; i < num_roads && std::getline(input_file, buffer_str); ++i) {
uint64_t shop_id, road_to;
if (std::sscanf(buffer_str.data(), "%lu %lu", &shop_id, &road_to) != 2) {
std::cerr << "parsing error: unexpected char stray - \"" << buffer_str << "\"" << std::endl;
std::terminate();
}
if (shop_id < 1 || shop_id > 1000) {
std::cerr << "warning: identifier for shop at line " << i + 2 << " is not in range 1 to 1000 inclusive: " << shop_id << std::endl;
continue;
}
try {
thomas::shop *shop = network.shop_at(shop_id);
try {
thomas::shop *dest = network.shop_at(road_to);
dest->get_connected_shops().push_back(shop);
shop->get_connected_shops().push_back(dest);
} catch (std::out_of_range &oor2) {
DEBUG_STREAM << "line " << i + 2 << ": destination shop with identifier " << road_to << " is being instantiated" << std::endl;
thomas::shop *new_dest = new thomas::shop(road_to);
new_dest->get_connected_shops().push_back(shop);
shop->get_connected_shops().push_back(new_dest);
network.register_shop(new_dest);
}
} catch (std::out_of_range &oor) {
DEBUG_STREAM << "line " << i + 2 << ": source shop with identifier " << shop_id << " is being instantiated" << std::endl;
thomas::shop *new_shop = new thomas::shop(shop_id);
try {
thomas::shop *dest = network.shop_at(road_to);
dest->get_connected_shops().push_back(new_shop);
new_shop->get_connected_shops().push_back(dest);
} catch (std::out_of_range &oor2) {
DEBUG_STREAM << "line " << i + 2 << ": destination shop with identifier " << road_to << " is being instantiated" << std::endl;
thomas::shop *new_dest = new thomas::shop(road_to);
new_dest->get_connected_shops().push_back(new_shop);
new_shop->get_connected_shops().push_back(new_dest);
network.register_shop(new_dest);
}
network.register_shop(new_shop);
}
}
DEBUG_STREAM << network;
DEBUG_STREAM << "network size: " << network.number_of_connections() << std::endl;
std::cout << network.reduce() << std::endl;
return 0;
}