From e973a7925aa06241bc03ab30f4d337c77bfe988f Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 3 Dec 2022 10:47:53 +0000 Subject: Tidy up day 3 --- src/day3.rs | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'src/day3.rs') diff --git a/src/day3.rs b/src/day3.rs index 5c01b7a..d19d51f 100644 --- a/src/day3.rs +++ b/src/day3.rs @@ -1,34 +1,29 @@ use std::{collections::{HashSet, HashMap}}; pub fn run(input: String) { - if !input.is_ascii() { - panic!("input wasn't ascii, character calculations won't work!"); - } - let scores_table = make_scores_table(); - - let ll: Vec<&str> = input.lines().collect(); - - let score = ll.chunks(3).map(|gm| { - let init: Option> = None; - let lb: Vec = gm.into_iter().fold(init, |maybe_acc, m| { - let cs: HashSet = m.chars().collect(); - if let Some(acc) = maybe_acc { - let rr: HashSet = acc.intersection(&cs).map(|x| {x.clone()}).collect(); - Some(rr) - } else { - Some(cs) - } - }) - .expect("didn't get any group members??!") - .into_iter().collect(); - let ct = lb.len(); + 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, gm); + panic!("Couldn't find exactly one badge for group! found {}, {:?}", ct, group); } - let ch = lb.into_iter().next().expect("foo"); - scores_table.get(&ch).expect(format!("couldn't find score for char {}", ch).as_str()) + scores_table[&overlapping_chars[0]] }).sum::(); + println!("Day 3: {}", score); } -- cgit v1.2.3-ZIG