summaryrefslogtreecommitdiffstats
path: root/2022/day3/part2/src
diff options
context:
space:
mode:
authorYigit Sever2022-12-04 14:19:12 +0300
committerYigit Sever2022-12-04 14:19:12 +0300
commitef7e8df2825480f34c1034015a8221c09f6ebdf6 (patch)
tree9ce122c24814d377d993a984178bfa42eac904df /2022/day3/part2/src
parent9725ad032ce7d42f01119d8334d420a398d2c855 (diff)
downloadaoc-ef7e8df2825480f34c1034015a8221c09f6ebdf6.tar.gz
aoc-ef7e8df2825480f34c1034015a8221c09f6ebdf6.tar.bz2
aoc-ef7e8df2825480f34c1034015a8221c09f6ebdf6.zip
2022, day3: done
Diffstat (limited to '2022/day3/part2/src')
-rw-r--r--2022/day3/part2/src/main.rs31
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 @@
1use itertools::Itertools;
2use std::collections::HashMap;
3use std::collections::HashSet;
4
5fn 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}