summary refs log tree commit diff
diff options
context:
space:
mode:
authordzwdz2022-12-04 11:48:22 +0100
committerdzwdz2022-12-04 11:48:22 +0100
commitb6bdf02ea469d71c26cd4d5e14b9dec122070a6b (patch)
tree45c59c507d1751d639fbcec9c5ed326e0c9f22f5
parent8be933143d9c3bca3a5473278bb8e31496ef108d (diff)
day 4: c++ classes
-rw-r--r--22.4/main.cpp43
1 files 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 <cstdio>
 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);