summaryrefslogtreecommitdiffstats
path: root/2022/day3/part2/src/main.rs
blob: 7139eb00c8c25c7738a7c625875154d555d54fe8 (plain)
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
use itertools::Itertools;
use std::collections::HashMap;
use std::collections::HashSet;

fn main() {
    let mut sum = 0;
    for group in &include_str!("../../input").lines().chunks(3) {
        let mut membership: HashMap<char, usize> = HashMap::new();
        for rucksack in group {
            let mut items: HashSet<char> = HashSet::new();
            for item in &rucksack.chars().chunks(2) {
                for c in item {
                    items.insert(c);
                }
            }
            for c in &items {
                *membership.entry(c.to_owned()).or_default() += 1;
            }
        }
        for (item, value) in membership {
            if value == 3 {
                if item.is_lowercase() {
                    sum += item as i16 - 96;
                } else {
                    sum += item as i16 - 38;
                }
            }
        }
    }
    println!("{}", sum);
}