summaryrefslogtreecommitdiff
path: root/04/src/part-2.rs
diff options
context:
space:
mode:
Diffstat (limited to '04/src/part-2.rs')
-rw-r--r--04/src/part-2.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/04/src/part-2.rs b/04/src/part-2.rs
new file mode 100644
index 0000000..5c12d21
--- /dev/null
+++ b/04/src/part-2.rs
@@ -0,0 +1,71 @@
+use std::cmp::{Ordering, PartialOrd};
+use std::io::{BufRead};
+use std::str::FromStr;
+
+#[derive(Debug, Eq, PartialEq)]
+struct Interval {
+ a: usize,
+ b: usize
+}
+
+impl PartialOrd for Interval {
+ fn partial_cmp(self: & Self, other: & Self) -> Option<Ordering> {
+ if self == other {
+ Some(Ordering::Equal)
+ }
+ else if self.a >= other.a && self.b <= other.b {
+ Some(Ordering::Less)
+ }
+ else if self.a <= other.a && self.b >= other.b {
+ Some(Ordering::Greater)
+ }
+ else {
+ None
+ }
+ }
+}
+
+impl Interval {
+ fn overlaps(self: & Self, other : & Self) -> bool {
+ self == other
+ || self <= other
+ || self >= other
+ || (self.a <= other.a && self.b >= other.a)
+ || (self.b >= other.b && self.a <= other.b)
+ }
+
+}
+
+impl FromStr for Interval {
+ type Err = std::string::ParseError;
+
+ fn from_str(s: & str) -> Result<Self, Self::Err> {
+ let mut split = s.split('-');
+ let a: usize = split.next().expect("FIXME: Turn into actual error.").parse().expect("FIXME");
+ let b: usize = split.next().expect("FIXME: Turn into actual error.").parse().expect("FIXME");
+
+ assert!(a <= b);
+
+ Ok(Self { a: a, b: b })
+ }
+}
+
+fn main() {
+ let stdin = std::io::stdin();
+ let handle = stdin.lock();
+
+ let mut count: usize = 0;
+
+ for l in handle.lines() {
+ let line = l.unwrap();
+ let mut split = line.split(',');
+ let i_1 = split.next().unwrap().parse::<Interval>().unwrap();
+ let i_2 = split.next().unwrap().parse::<Interval>().unwrap();
+
+ if i_1.overlaps(& i_2) {
+ count += 1;
+ }
+ }
+
+ println!("{}", count);
+}