Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashSet;
- fn score_char(letter: &char) -> u32 {
- let i = *letter as u32;
- if i >= 97 {
- i - 96
- } else {
- i - 38
- }
- }
- pub fn part_one(input: &str) -> Option<u32> {
- let mut priorities: u32 = 0;
- for line in input.lines() {
- let pivot = line.len() / 2;
- let compartment_a: HashSet<char> = HashSet::from_iter(line[0..pivot].chars());
- for c in line[pivot..].chars() {
- if compartment_a.contains(&c) {
- priorities += score_char(&c);
- break;
- }
- }
- }
- Some(priorities)
- }
- pub fn part_two(input: &str) -> Option<u32> {
- let mut priorities: u32 = 0;
- let mut set1: HashSet<char> = HashSet::with_capacity(50);
- let mut set2: HashSet<char> = HashSet::with_capacity(50);
- let mut set3: HashSet<char> = HashSet::with_capacity(50);
- let mut set4: HashSet<char> = HashSet::with_capacity(50);
- for lines in input.lines().collect::<Vec<&str>>().chunks_exact_mut(3) {
- let mut l = lines.iter_mut();
- for c in l.next().unwrap().chars() {
- set1.insert(c);
- }
- for c in l.next().unwrap().chars() {
- set2.insert(c);
- }
- for c in l.next().unwrap().chars() {
- set3.insert(c);
- }
- for c in set1.intersection(&set2) {
- set4.insert(*c);
- }
- priorities += score_char(set3.intersection(&set4).next().unwrap());
- set1.clear();
- set2.clear();
- set3.clear();
- set4.clear();
- }
- Some(priorities)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement