summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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);
}