summaryrefslogtreecommitdiffstats
path: root/src/bin/2020/day6.rs
blob: 114d05f289ccec1e25392beb781f3ad86027734a (plain) (blame)
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
use advent_of_code::prelude::*;

pub fn parse(fh: File) -> Result<impl Iterator<Item = String>> {
    Ok(parse::raw_lines(fh))
}

pub fn part1(lines: impl Iterator<Item = String>) -> Result<usize> {
    let mut yes = HashSet::new();
    let mut total = 0;
    for line in lines {
        if line.is_empty() {
            total += yes.len();
            yes = HashSet::new();
        } else {
            for c in line.chars() {
                yes.insert(c);
            }
        }
    }
    total += yes.len();
    Ok(total)
}

pub fn part2(lines: impl Iterator<Item = String>) -> Result<usize> {
    let mut yes = HashSet::new();
    for c in 'a'..='z' {
        yes.insert(c);
    }
    let mut total = 0;
    for line in lines {
        if line.is_empty() {
            total += yes.len();
            yes = HashSet::new();
            for c in 'a'..='z' {
                yes.insert(c);
            }
        } else {
            for c in 'a'..='z' {
                if !line.contains(c) {
                    yes.remove(&c);
                }
            }
        }
    }
    total += yes.len();
    Ok(total)
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(parse::data(2020, 6).unwrap()).unwrap()).unwrap(),
        6930
    );
    assert_eq!(
        part2(parse(parse::data(2020, 6).unwrap()).unwrap()).unwrap(),
        3585
    );
}