diff options
author | Martin Ashby <martin@ashbysoft.com> | 2022-12-03 10:47:53 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2022-12-03 10:47:53 +0000 |
commit | e973a7925aa06241bc03ab30f4d337c77bfe988f (patch) | |
tree | 71905dffd222d5e7fae4a89ccf36b0ab1cdd0b26 /src | |
parent | a1e913e617bf6b2637c0cac56adabe980d27387f (diff) | |
download | aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.tar.gz aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.tar.bz2 aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.tar.xz aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.zip |
Tidy up day 3
Diffstat (limited to 'src')
-rw-r--r-- | src/day3.rs | 43 |
1 files changed, 19 insertions, 24 deletions
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<HashSet<char>> = None; - let lb: Vec<char> = gm.into_iter().fold(init, |maybe_acc, m| { - let cs: HashSet<char> = m.chars().collect(); - if let Some(acc) = maybe_acc { - let rr: HashSet<char> = 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::<Vec<&str>>() + .chunks(3).map(|group| { + let init: Option<HashSet<char>> = None; + let overlapping_chars: Vec<char> = group.into_iter().fold(init, |acc, group_member| { + let group_member_charset: HashSet<char> = group_member.chars().collect(); + if let Some(acc) = acc { + let intersected: HashSet<char> = 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::<u32>(); + println!("Day 3: {}", score); } |