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
|
#![allow(dead_code)]
#![allow(unused_variables)]
use advent_of_code::prelude::*;
pub fn parse(
fh: File,
) -> Result<impl Iterator<Item = (HashSet<char>, HashSet<char>)>> {
Ok(parse::raw_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<u32> {
Ok(sacks
.map(|(first, second)| {
priority(*first.intersection(&second).next().unwrap())
})
.sum())
}
pub fn part2(
mut sacks: impl Iterator<Item = (HashSet<char>, HashSet<char>)>,
) -> Result<u32> {
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 +=
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
);
}
|