#![feature(iter_next_chunk)] use std::collections::HashSet; fn main() { part1(); part2(); } fn part1() { let input = include_str!("../input"); let ans: u32 = input.lines().map(|line| { let first = &line[..line.len()/2]; let second = &line[line.len()/2..]; let first: HashSet = first.chars().collect(); let second: HashSet = second.chars().collect(); let common = *first.intersection(&second).next().unwrap(); let priority = match common { 'a'..='z' => 1 + common as u32 - 'a' as u32, 'A'..='Z' => 27 + common as u32 - 'A' as u32, _ => unreachable!(), }; priority }).sum(); println!("{}", ans); } fn part2() { let input = include_str!("../input"); let mut lines = input.lines(); let mut sum = 0; while let Ok([first, second, third]) = lines.next_chunk::<3>() { let first: HashSet = first.chars().collect(); let second: HashSet = second.chars().collect(); let third: HashSet = third.chars().collect(); let common = *first .intersection(&second) .copied() .collect::>() .intersection(&third) .next() .unwrap(); let priority = match common { 'a'..='z' => 1 + common as u32 - 'a' as u32, 'A'..='Z' => 27 + common as u32 - 'A' as u32, _ => unreachable!(), }; sum += priority; } println!("{}", sum); }