summaryrefslogtreecommitdiffstats
path: root/src/2022/3/mod.rs
blob: b6c339eb2a1decb4b39a6682ad50e760739f6fd0 (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
#![allow(dead_code)]
#![allow(unused_variables)]

use crate::prelude::*;

pub fn parse(
    fh: File,
) -> Result<impl Iterator<Item = (HashSet<char>, HashSet<char>)>> {
    Ok(parse::lines(fh).map(|line| {
        let (first, second) = line.split_at(line.len() / 2);
        let first: HashSet<char> = first.chars().collect();
        let second: HashSet<char> = second.chars().collect();
        (first, second)
    }))
}

pub fn part1(
    sacks: impl Iterator<Item = (HashSet<char>, HashSet<char>)>,
) -> Result<i64> {
    Ok(sacks
        .map(|(first, second)| {
            i64::from(priority(*first.intersection(&second).next().unwrap()))
        })
        .sum())
}

pub fn part2(
    mut sacks: impl Iterator<Item = (HashSet<char>, HashSet<char>)>,
) -> Result<i64> {
    let mut total = 0;
    while let (Some(first), Some(second), Some(third)) =
        (sacks.next(), sacks.next(), sacks.next())
    {
        let first: HashSet<char> = first.0.union(&first.1).copied().collect();
        let second: HashSet<char> =
            second.0.union(&second.1).copied().collect();
        let third: HashSet<char> = third.0.union(&third.1).copied().collect();
        total += i64::from(priority(
            *(&(&first & &second) & &third).iter().next().unwrap(),
        ));
    }
    Ok(total)
}

fn priority(c: char) -> u32 {
    match c {
        'a'..='z' => u32::from(c) - u32::from('a') + 1,
        'A'..='Z' => u32::from(c) - u32::from('A') + 27,
        _ => unreachable!(),
    }
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(parse::data(2022, 3).unwrap()).unwrap()).unwrap(),
        8493
    );
    assert_eq!(
        part2(parse(parse::data(2022, 3).unwrap()).unwrap()).unwrap(),
        2552
    );
}