summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2022-12-03 10:47:53 +0000
committerMartin Ashby <martin@ashbysoft.com>2022-12-03 10:47:53 +0000
commite973a7925aa06241bc03ab30f4d337c77bfe988f (patch)
tree71905dffd222d5e7fae4a89ccf36b0ab1cdd0b26
parenta1e913e617bf6b2637c0cac56adabe980d27387f (diff)
downloadaoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.tar.gz
aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.tar.bz2
aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.tar.xz
aoc2022-e973a7925aa06241bc03ab30f4d337c77bfe988f.zip
Tidy up day 3
-rw-r--r--src/day3.rs43
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);
}