main.rs (1437B)
1 use std::collections::HashSet; 2 3 fn get_priority(c: char) -> i32 { 4 match c { 5 'a'..='z' => c as i32 - 'a' as i32 + 1, 6 'A'..='Z' => c as i32 - 'A' as i32 + 27, 7 _ => unreachable!(), 8 } 9 } 10 11 fn main() { 12 let input = std::fs::read_to_string("input.txt") 13 .unwrap() 14 .trim() 15 .split("\n") 16 .map(|x| x.chars().collect()) 17 .collect::<Vec<Vec<char>>>(); 18 // 8185 19 println!("Part 1: {}", part_1(&input)); 20 // 2817 21 println!("Part 2: {}", part_2(&input)); 22 } 23 24 fn part_1(input: &Vec<Vec<char>>) -> i32 { 25 input 26 .iter() 27 .map(|cv| { 28 let first = cv[0..(cv.len() / 2)] 29 .iter() 30 .map(|&x| x) 31 .collect::<HashSet<char>>(); 32 get_priority( 33 *cv[(cv.len() / 2)..cv.len()] 34 .iter() 35 .find(|c| first.contains(c)) 36 .unwrap(), 37 ) 38 }) 39 .sum() 40 } 41 42 fn part_2(input: &Vec<Vec<char>>) -> i32 { 43 input 44 .chunks(3) 45 .map(|cvv| { 46 let first = cvv[0].iter().map(|&x| x).collect::<HashSet<char>>(); 47 let second = cvv[1] 48 .iter() 49 .filter(|c| first.contains(c)) 50 .map(|&x| x) 51 .collect::<HashSet<char>>(); 52 get_priority(*cvv[2].iter().find(|c| second.contains(c)).unwrap()) 53 }) 54 .sum() 55 }