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
use std::error::Error;
use std::io::{self, Read};

fn main() -> Result<(), Box<Error>> {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input)?;

    let output = solve(&input);
    println!("{}", output);

    Ok(())
}

fn solve(input: &str) -> String {
    let box_ids: Vec<_> = input.lines().map(BoxId::new).collect();

    let two_count = box_ids.iter().filter(|&x| x.is_two()).count();
    let three_count = box_ids.iter().filter(|&x| x.is_three()).count();

    (two_count * three_count).to_string()
}

struct BoxId(String);

impl BoxId {
    fn new(s: &str) -> Self {
        BoxId(s.into())
    }

    fn is_two(&self) -> bool {
        self.is_n(2)
    }

    fn is_three(&self) -> bool {
        self.is_n(3)
    }

    fn is_n(&self, n: usize) -> bool {
        self.0
            .chars()
            .any(|x| self.0.chars().filter(|&y| x == y).count() == n)
    }
}

#[test]
fn test_is_two() {
    assert_eq!(BoxId::new("abcdef").is_two(), false);
    assert_eq!(BoxId::new("bababc").is_two(), true);
    assert_eq!(BoxId::new("abbcde").is_two(), true);
    assert_eq!(BoxId::new("abcccd").is_two(), false);
    assert_eq!(BoxId::new("aabcdd").is_two(), true);
    assert_eq!(BoxId::new("abcdee").is_two(), true);
    assert_eq!(BoxId::new("ababab").is_two(), false);
}

#[test]
fn test_is_three() {
    assert_eq!(BoxId::new("abcdef").is_three(), false);
    assert_eq!(BoxId::new("bababc").is_three(), true);
    assert_eq!(BoxId::new("abbcde").is_three(), false);
    assert_eq!(BoxId::new("abcccd").is_three(), true);
    assert_eq!(BoxId::new("aabcdd").is_three(), false);
    assert_eq!(BoxId::new("abcdee").is_three(), false);
    assert_eq!(BoxId::new("ababab").is_three(), true);
}