diff options
Diffstat (limited to 'src/day3.rs')
-rw-r--r-- | src/day3.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/day3.rs b/src/day3.rs new file mode 100644 index 0000000..5c01b7a --- /dev/null +++ b/src/day3.rs @@ -0,0 +1,46 @@ +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(); + if ct != 1 { + panic!("Couldn't find exactly one badge for group! found {}, {:?}", ct, gm); + } + let ch = lb.into_iter().next().expect("foo"); + scores_table.get(&ch).expect(format!("couldn't find score for char {}", ch).as_str()) + }).sum::<u32>(); + println!("Day 3: {}", score); +} + +fn make_scores_table() -> HashMap<char, u32> { + let mut scores_table: HashMap<char,u32> = 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 +}
\ No newline at end of file |