summaryrefslogtreecommitdiffstats
path: root/src/2020/2/mod.rs
blob: 88d3f5b14a065b33fe00c3d6c41f9a400d093a32 (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
60
61
62
63
64
use anyhow::Context as _;
use std::io::BufRead as _;

struct Line {
    c: char,
    min_times: usize,
    max_times: usize,
    password: String,
}

impl Line {
    fn parse(line: &str) -> anyhow::Result<Self> {
        let rx = regex::Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap();
        let captures =
            rx.captures(line).context("line failed to match regex")?;
        let c = captures
            .get(3)
            .unwrap()
            .as_str()
            .parse()
            .context("invalid policy char")?;
        let min_times = captures
            .get(1)
            .unwrap()
            .as_str()
            .parse()
            .context("invalid policy lower bound")?;
        let max_times = captures
            .get(2)
            .unwrap()
            .as_str()
            .parse()
            .context("invalid policy upper bound")?;
        let password = captures.get(4).unwrap().as_str().to_string();
        Ok(Self {
            c,
            min_times,
            max_times,
            password,
        })
    }

    fn valid(&self) -> bool {
        let count = self.password.chars().filter(|c| *c == self.c).count();
        count >= self.min_times && count <= self.max_times
    }
}

pub fn part1() -> anyhow::Result<()> {
    let f = std::fs::File::open("data/2.txt")
        .context("couldn't find data file 2.txt")?;
    let f = std::io::BufReader::new(f);
    let lines = f
        .lines()
        .map(|l| Line::parse(&l.context("failed to read a line")?))
        .collect::<anyhow::Result<Vec<Line>>>()?;
    let count = lines.iter().filter(|l| l.valid()).count();
    println!("{}", count);
    Ok(())
}

pub fn part2() -> anyhow::Result<()> {
    todo!()
}