-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (57 loc) · 1.4 KB
/
main.cpp
File metadata and controls
64 lines (57 loc) · 1.4 KB
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "utils.h" // Include the utils.h file
using namespace std;
int main() {
vector<pair<unsigned int, unsigned int>> ranges;
vector<pair<unsigned int, unsigned int>> out_ranges;
int iterations = 0;
string line;
// Input
while (getline(cin, line)) {
if (line.find('#') != string::npos) {
line = line.substr(0, line.find('#'));
}
if (line.empty()) continue;
try {
ranges.push_back(cidr_to_range(line));
} catch (const invalid_argument& ex) {
cerr << "ERROR: " << ex.what() << endl;
return 1;
}
}
// Merge
while (true) {
iterations++;
sort(ranges.begin(), ranges.end());
for (auto& a : ranges) {
for (const auto& b : ranges) {
auto result = compare_ranges(a, b);
if (result) {
a = *result;
}
}
if (find(out_ranges.begin(), out_ranges.end(), a) == out_ranges.end()) {
out_ranges.push_back(a);
}
}
if (ranges == out_ranges) break;
ranges = out_ranges;
out_ranges.clear();
}
// Output
sort(out_ranges.begin(), out_ranges.end());
for (const auto& r : out_ranges) {
try {
for (const auto& c : range_to_cidrs(r)) {
cout << c << endl;
}
} catch (const invalid_argument& ex) {
cerr << "ERROR: " << ex.what() << endl;
return 1;
}
}
return 0;
}