use std::{collections::{HashSet, HashMap}}; pub fn run(input: String) { let scores_table = make_scores_table(); let score = input.lines() .collect::>() .chunks(3).map(|group| { let init: Option> = None; let overlapping_chars: Vec = group.into_iter().fold(init, |acc, group_member| { let group_member_charset: HashSet = group_member.chars().collect(); if let Some(acc) = acc { let intersected: HashSet = acc.intersection(&group_member_charset).map(|x| {x.clone()}).collect(); Some(intersected) } else { Some(group_member_charset) } }) .expect("didn't get any group members??!") .into_iter().collect(); let ct = overlapping_chars.len(); if ct != 1 { panic!("Couldn't find exactly one badge for group! found {}, {:?}", ct, group); } scores_table[&overlapping_chars[0]] }).sum::(); println!("Day 3: {}", score); } fn make_scores_table() -> HashMap { let mut scores_table: HashMap = HashMap::new(); let r1 = 'a'..='z'; for (ix, ch) in r1.enumerate() { scores_table.insert(ch, (ix as u32)+1); } let r2 = 'A'..='Z'; for (ix, ch) in r2.enumerate() { scores_table.insert(ch, (ix as u32)+27); } scores_table }