aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day4/aoc.h
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-04 13:31:56 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-04 13:31:56 +0800
commitbaf9117b3f11a03f21f37a5abb3ef105dff4652f (patch)
tree737df399dec0b68cf32eec0abb05b1dc76ef252a /src/2022/day4/aoc.h
parentf69e238bec1b742fcb17709ab16f6ccf3d4ed514 (diff)
downloadadvent-of-code-baf9117b3f11a03f21f37a5abb3ef105dff4652f.tar.gz
advent-of-code-baf9117b3f11a03f21f37a5abb3ef105dff4652f.zip
2022 day4
Diffstat (limited to 'src/2022/day4/aoc.h')
-rw-r--r--src/2022/day4/aoc.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/2022/day4/aoc.h b/src/2022/day4/aoc.h
index 3b8a1ec..b291262 100644
--- a/src/2022/day4/aoc.h
+++ b/src/2022/day4/aoc.h
@@ -3,6 +3,50 @@
namespace aoc2022 {
+struct apair {
+ int first[2] = {0};
+ int second[2] = {0};
+
+ void get_number(int* d, int i, const char* p) {
+ *(d + i) = *(d + i) * 10 + *p - '0';
+ }
+
+ void load(line_view lv) {
+ int* d = first;
+ int i{0};
+ const char* p0 = lv.line;
+ while (p0 < lv.line + lv.length) {
+ if (*p0 >= '0' && *p0 <= '9') {
+ get_number(d, i, p0);
+ }
+ else {
+ if (*p0 == ',') {
+ d = second;
+ i = 0;
+ }
+ if (*p0 == '-') {
+ i += 1;
+ }
+ }
+ p0++;
+ }
+ }
+
+ bool covered() const noexcept {
+ bool b1 = first[0] <= second[0] && first[1] >= second[1];
+ bool b2 = second[0] <= first[0] && second[1] >= first[1];
+ return b1 || b2;
+ }
+
+ bool overlap() const noexcept {
+ bool b1 = second[0] >= first[0] && second[0] <= first[1];
+ bool b2 = second[1] >= first[0] && second[1] <= first[1];
+ bool b3 = first[0] >= second[0] && first[0] <= second[1];
+ bool b4 = first[1] >= second[0] && first[1] <= second[1];
+ return covered() || b1 || b2 || b3 || b4;
+ }
+};
+
std::pair<int, int> day4(line_view file);
}