You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.4 KiB
Rust

use crate::util::prelude::*;
static INPUT: &str = include_str!("inputs/day3.txt");
type Item = char;
pub fn item_prio(item: &Item) -> uint {
if ('a'..='z').contains(&item) {
return (*item as uint - 'a' as uint) + 1;
}
if ('A'..='Z').contains(&item) {
return (*item as uint - 'A' as uint) + 27;
}
panic!()
}
pub fn part1() {
let sum = INPUT
.split('\n')
.filter(|s| !s.is_empty())
.map(|line| {
let len = line.len();
let (fst_ruck_str, snd_ruck_str) = line.split_at(len / 2);
let fst_ruck = fst_ruck_str.chars().collect::<HashSet<_>>();
let snd_ruck = snd_ruck_str.chars().collect::<HashSet<_>>();
let both = fst_ruck.intersection(&snd_ruck).collect::<HashSet<_>>();
assert!(both.len() == 1);
let both = both.iter().next().unwrap();
item_prio(*both)
})
.sum::<uint>();
println!("{}", sum);
}
pub fn part2() {
let lines: Vec<_> = INPUT.split('\n').filter(|s| !s.is_empty()).collect();
let elf_groups = lines.chunks_exact(3);
let sum: uint = elf_groups
.flat_map(|lines| {
let item_counts = lines
.iter()
.map(|line| line.chars().collect::<HashSet<_>>())
.fold(HashMap::default(), |mut map, set| {
for v in set.iter() {
let ct = map.entry(*v).or_insert(0);
*ct += 1;
}
map
});
item_counts
.iter()
.max_by(|a, b| a.1.cmp(b.1))
.map(|v| item_prio(v.0))
})
.sum();
println!("{}", sum);
}