diff options
Diffstat (limited to '2022/day3/part2/src/main.rs')
-rw-r--r-- | 2022/day3/part2/src/main.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/2022/day3/part2/src/main.rs b/2022/day3/part2/src/main.rs new file mode 100644 index 0000000..7139eb0 --- /dev/null +++ b/2022/day3/part2/src/main.rs | |||
@@ -0,0 +1,31 @@ | |||
1 | use itertools::Itertools; | ||
2 | use std::collections::HashMap; | ||
3 | use std::collections::HashSet; | ||
4 | |||
5 | fn main() { | ||
6 | let mut sum = 0; | ||
7 | for group in &include_str!("../../input").lines().chunks(3) { | ||
8 | let mut membership: HashMap<char, usize> = HashMap::new(); | ||
9 | for rucksack in group { | ||
10 | let mut items: HashSet<char> = HashSet::new(); | ||
11 | for item in &rucksack.chars().chunks(2) { | ||
12 | for c in item { | ||
13 | items.insert(c); | ||
14 | } | ||
15 | } | ||
16 | for c in &items { | ||
17 | *membership.entry(c.to_owned()).or_default() += 1; | ||
18 | } | ||
19 | } | ||
20 | for (item, value) in membership { | ||
21 | if value == 3 { | ||
22 | if item.is_lowercase() { | ||
23 | sum += item as i16 - 96; | ||
24 | } else { | ||
25 | sum += item as i16 - 38; | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | println!("{}", sum); | ||
31 | } | ||