-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.rs
34 lines (27 loc) · 1.03 KB
/
04.rs
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
use std::collections::HashSet;
type Set = HashSet<u32>;
fn count_overlaps_where(input: &str, overlap_fn: &dyn Fn(&Set, &Set) -> bool) -> usize {
input
.lines()
.filter(|assignments| {
let assignments: Vec<u32> = assignments
.split(['-', ','])
.flat_map(|n| n.parse())
.collect();
let left_set: HashSet<u32> = (assignments[0]..=assignments[1]).collect();
let right_set: HashSet<u32> = (assignments[2]..=assignments[3]).collect();
overlap_fn(&left_set, &right_set) || overlap_fn(&right_set, &left_set)
})
.count()
}
fn part_one(input: &str) -> usize {
count_overlaps_where(input, &|set_a, set_b| set_a.is_subset(set_b))
}
fn part_two(input: &str) -> usize {
count_overlaps_where(input, &|set_a, set_b| !set_a.is_disjoint(set_b))
}
pub fn main() {
let input = include_str!("../inputs/04.txt");
println!("part one: {}", part_one(input)); // 524
println!("part two: {}", part_two(input)); // 798
}