summaryrefslogtreecommitdiff
path: root/04/src/part-2.rs
blob: 5c12d21a43a625cdbca78a5b3b0c5a5909607bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
}