Skip to main content

aoc/year2016/
day20.rs

1//! # Firewall Rules
2use crate::util::iter::*;
3use crate::util::parse::*;
4
5type Range = [u64; 2];
6
7/// The trick to merge ranges efficiently is to sort by the *starting* index.
8pub fn parse(input: &str) -> Vec<Range> {
9    let mut ranges: Vec<_> = input.iter_unsigned().chunk::<2>().collect();
10    ranges.sort_unstable_by_key(|r| r[0]);
11    ranges
12}
13
14pub fn part1(input: &[Range]) -> u64 {
15    let mut index = 0;
16
17    for &[start, end] in input {
18        if index < start {
19            return index;
20        }
21        index = index.max(end + 1);
22    }
23
24    unreachable!()
25}
26
27pub fn part2(input: &[Range]) -> u64 {
28    let mut index = 0;
29    let mut total = 0;
30
31    for &[start, end] in input {
32        if index < start {
33            total += start - index;
34        }
35        index = index.max(end + 1);
36    }
37
38    total
39}