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
127
128
129
130
131
132
133
134
135
//! This is my solution for [Advent of Code - Day 4 - _Camp Cleanup_](https://adventofcode.com/2022/day/4)
//!
//! Today is comparing the ranges of the base camp pairs of elves have to clean to check for redundancy.

/// Represents a length of beach an elf is assigned to clean
type Range = (u32, u32);
/// The assignments for a pair of elves
type Pair = (Range, Range);

use std::fs;

/// The entry point for running the solutions with the 'real' puzzle input.
///
/// - The puzzle input is expected to be at `<project_root>/res/day-4-input`
/// - It is expected this will be called by [`super::main()`] when the user elects to run day 4.
pub fn run() {
    let contents = fs::read_to_string("res/day-4-input").expect("Failed to read file");
    let pairs = parse_input(&contents);

    println!(
        "There are {} redundant pairs of elves",
        count_pairs_matching(&pairs, pair_has_redundant_elf)
    );

    println!(
        "There are {} overlapping pairs of elves",
        count_pairs_matching(&pairs, pair_overlaps)
    );
}

/// Parse the string puzzle inut into a list of elf pairs
fn parse_input(input: &String) -> Vec<Pair> {
    input.lines().map(parse_line).collect()
}

/// Parse a lne of the input as a single pair of elves
fn parse_line(line: &str) -> Pair {
    let parts: Vec<&str> = line.split(',').collect();
    (
        parse_range(parts[0]),
        parse_range(parts[1])
    )
}

/// Parse the bounds of the range assigned to one elf
fn parse_range(spec: &str) -> Range {
    let limits: Vec<u32> =
        spec.split('-')
            .map(|str| str.parse::<u32>().unwrap())
            .collect();

    (limits[0], limits[1])
}

/// Predicate for counting pairs that wholly overlao
fn pair_has_redundant_elf(((elf1_start, elf1_end), (elf2_start, elf2_end)): Pair) -> bool {
    (elf1_start <= elf2_start && elf1_end >= elf2_end) ||
        (elf1_start >= elf2_start && elf1_end <= elf2_end)
}

/// Predicate for counting pairs that wholly or partially overlap
fn pair_overlaps(((elf1_start, elf1_end), (elf2_start, elf2_end)): Pair) -> bool {
    elf1_start <= elf2_end && elf1_end >= elf2_start
}

/// Given a list of elf pairs, count those that match the given predicate
fn count_pairs_matching(pairs: &Vec<Pair>, predicate: fn(Pair) -> bool) -> usize {
    pairs.iter().filter(|&&pair| predicate(pair)).count()
}

#[cfg(test)]
mod tests {
    use crate::day_4::{count_pairs_matching, Pair, pair_has_redundant_elf, pair_overlaps, parse_input};

    fn sample_pairs() -> Vec<Pair> {
        vec![
            ((2, 4), (6, 8)),
            ((2, 3), (4, 5)),
            ((5, 7), (7, 9)),
            ((2, 8), (3, 7)),
            ((6, 6), (4, 6)),
            ((2, 6), (4, 8)),
        ]
    }

    #[test]
    fn can_parse() {
        let input = "2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8".to_string();

        assert_eq!(parse_input(&input), sample_pairs());
    }

    #[test]
    fn can_find_redundant_pairs() {
        for (pair, expected) in sample_pairs().into_iter().zip(vec![false, false, false, true, true, false]) {
            assert_eq!(pair_has_redundant_elf(pair), expected, "Check pair {:?} redundancy", pair);
        }

        let boundaries = vec![
            ((4, 4), (4, 6)),
            ((6, 6), (4, 6)),
            ((4, 6), (4, 4)),
            ((4, 6), (6, 6)),
        ];
        for pair in boundaries {
            assert_eq!(pair_has_redundant_elf(pair), true, "Check pair {:?} redundancy", pair);
        }
    }

    #[test]
    fn can_find_overlaps_pairs() {
        let possibilities = vec![
            (((4, 6), (5, 5)), true),
            (((5, 5), (4, 6)), true),
            (((4, 5), (5, 6)), true),
            (((5, 6), (4, 5)), true),
            (((4, 5), (6, 7)), false),
            (((6, 7), (4, 5)), false),
        ];
        for (pair, expected) in possibilities {
            assert_eq!(pair_overlaps(pair), expected, "Check pair {:?} overlap", pair);
        }
    }

    #[test]
    fn can_count_pairs() {
        assert_eq!(count_pairs_matching(&sample_pairs(), pair_has_redundant_elf), 2);
        assert_eq!(count_pairs_matching(&sample_pairs(), pair_overlaps), 4);
    }
}