summaryrefslogtreecommitdiff
path: root/2022/day4/src/lib.rs
blob: e64021b4200ad558511d0f91e26308ee0b9328f8 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::cmp::{max, min};
use std::convert::From;

#[derive(Debug)]
struct Range {
    start: i16,
    end: i16,
}

#[derive(Debug)]
struct Ranges {
    one: Range,
    two: Range,
}

impl From<&str> for Range {
    fn from(item: &str) -> Self {
        let dash_idx = item.find("-").expect("Can't find dash, can't parse.");
        let start: i16 = item[..dash_idx].parse().unwrap();
        let end: i16 = item[dash_idx + 1..].parse().unwrap();

        Range { start, end }
    }
}

impl From<&str> for Ranges {
    fn from(item: &str) -> Self {
        let comma_idx = item.find(",").expect("Comma not found, can't parse.");

        Ranges {
            one: item[..comma_idx].into(),
            two: item[comma_idx + 1..].into(),
        }
    }
}

impl Ranges {
    fn are_subset(&self) -> bool {
        let a = &self.one;
        let b = &self.two;

        if a.start <= b.start && a.end >= b.end {
            return true;
        }

        if b.start <= a.start && b.end >= a.end {
            return true;
        }

        false
    }

    fn are_overlap(&self) -> bool {
        let a = &self.one;
        let b = &self.two;

        return min(a.end, b.end) - max(a.start, b.start) >= 0;
    }
}

pub fn part1(input: &str) -> u32 {
    let ranges: Vec<Ranges> = input.trim().lines().map(|x| x.into()).collect();
    let mut tot: u32 = 0;

    for range in ranges {
        if range.are_subset() {
            tot += 1;
        }
    }

    tot
}

pub fn part2(input: &str) -> u32 {
    let ranges: Vec<Ranges> = input.trim().lines().map(|x| x.into()).collect();
    let mut tot: u32 = 0;

    for range in ranges {
        if range.are_subset() || range.are_overlap() {
            tot += 1;
        }
    }

    tot
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_example() {
        let data = include_str!("../example.txt");

        let result = part1(data);

        assert_eq!(result, 2);
    }

    #[test]
    fn test_input1() {
        let data = include_str!("../input.txt");

        let result = part1(data);

        assert_eq!(result, 595);
    }

    #[test]
    fn test_example2() {
        let data = include_str!("../example.txt");

        let result = part2(data);

        assert_eq!(result, 4);
    }

    #[test]
    fn test_input2() {
        let data = include_str!("../input.txt");

        let result = part2(data);

        assert_eq!(result, 952);
    }
}