-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_20b.cpp
64 lines (59 loc) · 1.77 KB
/
day_20b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Range {
long long start;
long long end;
};
int main(int argc, char* argv[]) {
std::string input = "../input/day_20_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<Range> ranges;
long long allowed = 0;
while(std::getline(file, line)) {
const auto dash_idx = line.find('-');
Range range;
range.start = std::stoll(line.substr(0, dash_idx));
range.end = std::stoll(line.substr(dash_idx + 1, line.size() - dash_idx - 1));
ranges.push_back(range);
}
std::sort(std::begin(ranges), std::end(ranges), [](const Range& r1, const Range& r2) { return r1.start < r2.start; });
int range_idx = 0;
for (std::size_t idx = 0; idx < ranges.size() - 1; idx++) {
if (ranges[range_idx].end >= ranges[idx+1].start) {
Range range;
range.start = ranges[range_idx].start;
range.end = (ranges[range_idx].end > ranges[idx + 1].end) ?
ranges[range_idx].end :
ranges[idx + 1].end;
ranges[range_idx] = range;
} else {
range_idx = idx+1;
}
}
std::vector<Range> combined_ranges{ranges[0]};
range_idx = 0;
for (std::size_t idx = 1; idx < ranges.size(); idx++) {
if (ranges[idx].start > ranges[range_idx].end) {
range_idx = idx;
combined_ranges.push_back(ranges[idx]);
}
}
std::swap(ranges, combined_ranges);
if (ranges[0].start != 0) {
allowed += ranges[0].start;
}
for (std::size_t idx = 0; idx < ranges.size() - 1; idx++) {
if (ranges[idx].end + 1 < ranges[idx+1].start) {
allowed += ranges[idx + 1].start - ranges[idx].end - 1;
}
}
std::cout << allowed << '\n';
return 0;
}