From b6bdf02ea469d71c26cd4d5e14b9dec122070a6b Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 4 Dec 2022 11:48:22 +0100 Subject: day 4: c++ classes --- 22.4/main.cpp | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/22.4/main.cpp b/22.4/main.cpp index cf7f9c0..df02c81 100644 --- a/22.4/main.cpp +++ b/22.4/main.cpp @@ -1,34 +1,39 @@ #include using namespace std; -bool -range_contains(int a1, int a2, int b1, int b2) -{ - return a1 <= b1 && b2 <= a2; -} +struct +Range { + int from, to; -bool -range_in(int a1, int a2, int v) -{ - return a1 <= v && v <= a2; -} + bool + contains(Range &other) { + return this->from <= other.from && other.to <= this->to; + } -bool -range_overlap(int a1, int a2, int b1, int b2) -{ - return range_in(a1, a2, b1) || range_in(a1, a2, b2) || range_in(b1, b2, a1) || range_in(b1, b2, a2); -} + bool + contains(int n) { + return this->from <= n && n <= this->to; + } + + bool + overlaps(Range &other) { + return this->contains(other.from) + || this->contains(other.to) + || other.contains(this->from) + || other.contains(this->to); + } +}; int main() { - int a1, a2, b1, b2; + Range a, b; int total1 = 0; int total2 = 0; - while (scanf("%d-%d,%d-%d ", &a1, &a2, &b1, &b2) == 4) { - if (range_contains(a1, a2, b1, b2) || range_contains(b1, b2, a1, a2)) + while (scanf("%d-%d,%d-%d ", &a.from, &a.to, &b.from, &b.to) == 4) { + if (a.contains(b) || b.contains(a)) total1++; - if (range_overlap(a1, a2, b1, b2)) + if (a.overlaps(b)) total2++; } printf("%d\n", total1); -- cgit 1.4.1-2-gfad0