-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.cpp
66 lines (51 loc) · 1.54 KB
/
4.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <ranges>
#include <regex>
#include <utility>
#include <vector>
std::regex regex{R"([-,]+)"};
bool contains(int x, int y, int a, int b) {
return (x <= a && y >= b) || (x >= a && y <= b);
}
bool overlap(int x, int y, int a, int b) {
return (x <= a && y >= a)
|| (a <= x && b >= x);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
return 1;
}
std::ifstream file(argv[1]);
int cnt_one = 0;
int cnt_two = 0;
for (std::string line; std::getline(file, line);) {
std::sregex_token_iterator it{line.begin(), line.end(), regex, -1};
std::vector<std::string> pos{it, {}};
std::vector<int> posi;
std::ranges::transform(pos, std::back_inserter(posi),
[](auto s) { return std::stoi(s); });
if (contains(posi[0], posi[1], posi[2], posi[3])) {
cnt_one++;
std::cout << "contains: ";
// before c++20
// std::copy(pos.begin(), pos.end(),
// std::ostream_iterator<std::string>(std::cout, " "));
// c++20
std::ranges::copy(pos,
std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
}
if (overlap(posi[0], posi[1], posi[2], posi[3])) {
cnt_two++;
std::cout << "overlap: ";
std::ranges::copy(pos,
std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
}
}
std::cout << cnt_one << std::endl;
std::cout << cnt_two << std::endl;
}