diff options
Diffstat (limited to 'src/2022/day4/aoc.h')
-rw-r--r-- | src/2022/day4/aoc.h | 44 |
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); } |