-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.rs
69 lines (59 loc) · 1.47 KB
/
day04.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
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
use std::collections::HashSet;
pub struct Pair {
one: HashSet<usize>,
two: HashSet<usize>,
}
fn fill_hash(range: &str) -> HashSet<usize> {
range
.split_once('-')
.and_then(|(start, end)| {
start.parse().ok().zip(end.parse().ok())
})
.map(|(start, end)| (start..=end).collect())
.unwrap()
}
#[aoc_generator(day4)]
pub fn input_generator(input: &str) -> Vec<Pair> {
input
.lines()
.map(|line| {
let (left, right) = line.split_once(',').unwrap();
Pair {
one: fill_hash(left),
two: fill_hash(right),
}
})
.collect()
}
#[aoc(day4, part1)]
pub fn solve_part1(input: &Vec<Pair>) -> usize {
input
.iter()
.filter(|pair| pair.one.is_subset(&pair.two) || pair.one.is_superset(&pair.two))
.count()
}
#[aoc(day4, part2)]
pub fn solve_part2(input: &Vec<Pair>) -> usize {
input
.iter()
.filter(|pair| !pair.one.is_disjoint(&pair.two))
.count()
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "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 part1_test() {
assert_eq!(solve_part1(&input_generator(TEST)), 2);
}
#[test]
fn part2_test() {
assert_eq!(solve_part2(&input_generator(TEST)), 4);
}
}