summaryrefslogtreecommitdiffstats
path: root/day04b/src/main.rs
blob: 31fc46f46c71a0e071b291f87c995bd443661e32 (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
/// --- Part Two ---
///
/// It seems like there is still quite a bit of duplicate work planned.  Instead, the Elves would
/// like to know the number of pairs that overlap at all.
///
/// In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the
/// remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:
///
///     5-7,7-9 overlaps in a single section, 7.
///     2-8,3-7 overlaps all of the sections 3 through 7.
///     6-6,4-6 overlaps in a single section, 6.
///     2-6,4-8 overlaps in sections 4, 5, and 6.
///
/// So, in this example, the number of overlapping assignment pairs is 4.
///
/// In how many assignment pairs do the ranges overlap?
use clap::Parser;
use itertools::Itertools;

use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;

const FILEPATH: &'static str = "examples/input.txt";

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
    #[clap(short, long, default_value = FILEPATH)]
    file: PathBuf,
}

fn main() {
    let args = Cli::parse();

    let file = File::open(&args.file).unwrap();
    let reader = BufReader::new(file);
    let res = reader
        .lines()
        .map(|l| {
            l.unwrap()
                .split(',')
                .map(|s| {
                    s.split('-')
                        .map(|v| v.parse::<usize>().unwrap())
                        .collect_tuple::<(usize, usize)>()
                        .unwrap()
                })
                .collect_tuple::<((usize, usize), (usize, usize))>()
                .unwrap()
        })
        .filter(|((l1, l2), (r1, r2))| !(l1 > r2 || r1 > l2))
        .count();

    println!("{res}");
}