From ef7e8df2825480f34c1034015a8221c09f6ebdf6 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 4 Dec 2022 14:19:12 +0300 Subject: 2022, day3: done --- 2022/day3/part2/src/main.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2022/day3/part2/src/main.rs (limited to '2022/day3/part2/src/main.rs') 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 @@ +use itertools::Itertools; +use std::collections::HashMap; +use std::collections::HashSet; + +fn main() { + let mut sum = 0; + for group in &include_str!("../../input").lines().chunks(3) { + let mut membership: HashMap = HashMap::new(); + for rucksack in group { + let mut items: HashSet = HashSet::new(); + for item in &rucksack.chars().chunks(2) { + for c in item { + items.insert(c); + } + } + for c in &items { + *membership.entry(c.to_owned()).or_default() += 1; + } + } + for (item, value) in membership { + if value == 3 { + if item.is_lowercase() { + sum += item as i16 - 96; + } else { + sum += item as i16 - 38; + } + } + } + } + println!("{}", sum); +} -- cgit v1.2.3-70-g09d2