This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4-camp-cleanup.cc
74 lines (59 loc) · 1.91 KB
/
day4-camp-cleanup.cc
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
#include "helper.hh"
#include <charconv>
#include <iostream>
#include <string_view>
using CleaningRange = std::pair<int, int>;
static CleaningRange ParseRange(const std::string_view range) {
const auto &&[first, second] = split(range, '-');
CleaningRange result{};
std::from_chars(first.data(), std::next(first.data(), first.size()),
result.first);
std::from_chars(second.data(), std::next(second.data(), second.size()),
result.second);
return result;
}
static std::pair<CleaningRange, CleaningRange>
ParseLine(const std::string_view line) {
const auto &&[first, second] = split(line, ',');
return {ParseRange(first), ParseRange(second)};
}
static bool Contains(const CleaningRange &container,
const CleaningRange &containee) {
return container.first <= containee.first &&
container.second >= containee.second;
}
static bool FullyOverlaps(const CleaningRange &lhs, const CleaningRange &rhs) {
return Contains(lhs, rhs) || Contains(rhs, lhs);
}
static bool PartiallyOverlaps(CleaningRange lhs, CleaningRange rhs) {
if (rhs.first < lhs.first) {
std::swap(lhs.first, rhs.first);
}
if (rhs.second < lhs.second) {
std::swap(lhs.second, rhs.second);
}
return lhs.second >= rhs.first && rhs.second >= lhs.first;
}
int main(const int argc, const char *argv[]) {
if (argc < 2) {
return 1;
}
auto fileStream = ReadFile(argv[1]);
if (!fileStream) {
std::cerr << "Failed to open or read file \'" << argv[1] << "\'\n";
return 1;
}
std::string line;
int overlaps[2]{};
while (std::getline(*fileStream, line) && !line.empty()) {
const auto &&[first, second] = ParseLine(line);
if (FullyOverlaps(first, second)) {
++overlaps[0];
}
if (PartiallyOverlaps(first, second)) {
++overlaps[1];
}
}
std::cout << "part1: " << overlaps[0] << '\n'
<< "part2: " << overlaps[1] << '\n';
}