54 lines
1.4 KiB
Rust
54 lines
1.4 KiB
Rust
use std::io;
|
|
use std::vec::Vec;
|
|
use std::collections::HashSet;
|
|
|
|
use crate::utils;
|
|
|
|
pub fn execute() -> Result<(), io::Error> {
|
|
let mut score = 0;
|
|
let mut sacks: Vec<HashSet<u8>> = Vec::new();
|
|
|
|
for line in utils::read_lines("input/day03.txt")? {
|
|
let text = line?;
|
|
let (text1, text2) = text.split_at(text.len() / 2);
|
|
let mut pouch1 = HashSet::new();
|
|
let mut pouch2 = HashSet::new();
|
|
for i in 0..text1.len() {
|
|
pouch1.insert(text1.as_bytes()[i]);
|
|
pouch2.insert(text2.as_bytes()[i]);
|
|
}
|
|
|
|
let intersect: HashSet<_> = pouch1.intersection(&pouch2).cloned().collect();
|
|
score += get_score(&intersect);
|
|
|
|
sacks.push(pouch1.union(&pouch2).cloned().collect());
|
|
}
|
|
|
|
let mut score2 = 0;
|
|
for group in sacks.chunks(3) {
|
|
let mut intersect: HashSet<u8> = group[0].intersection(&group[1]).cloned().collect();
|
|
intersect = intersect.intersection(&group[2]).cloned().collect();
|
|
score2 += get_score(&intersect);
|
|
}
|
|
|
|
utils::print_step(1, &score);
|
|
utils::print_step(2, &score2);
|
|
Ok(())
|
|
}
|
|
|
|
fn get_score(intersect: &HashSet<u8>) -> u64 {
|
|
if intersect.len() > 1 {
|
|
panic!("More than one overlapping item.");
|
|
}
|
|
|
|
if let Some(item) = intersect.iter().next() {
|
|
if *item > 0x60 {
|
|
return (*item - 0x60) as u64;
|
|
}
|
|
|
|
return ((*item - 0x40) + 26) as u64;
|
|
}
|
|
|
|
panic!("No items in intersection.");
|
|
}
|