aboutsummaryrefslogtreecommitdiff
path: root/2022/15/a.rs
blob: 7b871502b92aad600c3b632811848a2560515f3e (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
use std::cmp::max;
use std::collections::HashSet;
mod common;
use common::*;

fn main() {
    const Y: i64 = 2000000;
    let sensors = read_sensors_from_stdin();
    let mut ranges = sensors.iter()
        .map(|s| s.get_range(Y))
        .filter(|r| r.left <= r.right)
        .collect::<Vec<_>>();
    ranges.sort();
    let mut current = Range { left: i64::MIN, right: i64::MIN };
    let mut sum = 0;
    for r in ranges {
        current = Range {
            left: max(current.right+1, r.left),
            right: max(current.right, r.right)
        };
        sum += (current.right - current.left + 1) as usize;
    }
    sum -= sensors.iter()
        .map(|s| s.b)
        .collect::<HashSet<_>>() // Remove duplicates
        .iter()
        .filter(|b| b.y == Y)
        .count();
    println!("{sum}");
}

Generated with cgit - Back to sebastiano.tronto.net