-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.rs
56 lines (51 loc) · 1.61 KB
/
day03.rs
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
use std::collections::HashSet;
fn main() {
let rucksacks: Vec<&str> = include_str!("./input.txt").trim().lines().collect();
let a: u64 = rucksacks
.iter()
.map(|x| x.split_at(x.len() / 2))
.map(|(l, r)| {
(
HashSet::<_>::from_iter(l.chars()),
HashSet::<_>::from_iter(r.chars()),
)
})
.map(|(l, r)| {
l.intersection(&r)
.cloned()
.into_iter()
.fold(0u64, |acc, v| match v {
'a'..='z' => acc + 1 + (v as u64) - ('a' as u64),
'A'..='Z' => acc + 27 + (v as u64) - ('A' as u64),
_ => unreachable!(),
})
})
.sum();
println!("part1 {}", a);
let b: u64 = rucksacks
.chunks_exact(3)
.map(|chunk| {
chunk
.into_iter()
.map(|l| HashSet::from_iter(l.chars()))
.collect::<Vec<HashSet<char>>>()
})
.map(|groups| {
groups
.iter()
.skip(1)
.fold(groups[0].clone(), |acc, hs| {
acc.intersection(hs).cloned().collect()
})
.iter()
.fold(0u64, |acc, v| {
match v {
'a'..='z' => acc + 1 + (*v as u64) - ('a' as u64),
'A'..='Z' => acc + 27 + (*v as u64) - ('A' as u64),
_ => unreachable!(),
}
})
})
.sum();
println!("part2 {}", b);
}