This commit is contained in:
2022-12-03 14:53:06 +00:00
parent c4266bb16c
commit 2dc8abfbc6
4 changed files with 78 additions and 1 deletions
+52
View File
@@ -0,0 +1,52 @@
use rustc_hash::FxHashSet;
use std::io::{self, BufRead};
const LOWERCASE_ASCII_OFFSET: usize = 64;
const UPPERCASE_ASCII_OFFSET: usize = 32;
const LOWERCASE_ALPHA_OFFSET: usize = 26;
fn main() {
let lines = io::stdin().lock().lines().map(|l| l.unwrap());
let mut first_half: FxHashSet<char>;
let mut second_half: FxHashSet<char>;
let mut p1_sum = 0usize;
let mut p2_sum = 0usize;
let mut group_set: FxHashSet<char>;
let mut groups: Vec<String> = Vec::new();
for line in lines {
groups.push(line.clone());
if groups.len() == 3 {
group_set = groups[0]
.chars()
.collect::<FxHashSet<char>>()
.intersection(&groups[1].chars().collect())
.copied()
.collect::<FxHashSet<char>>()
.intersection(&groups[2].chars().collect())
.copied()
.collect();
p2_sum += type_to_priority(group_set.iter().next().unwrap());
groups.clear();
}
let (first, second) = line.split_at(line.len() / 2);
first_half = first.chars().collect();
second_half = second.chars().collect();
let shared_type = first_half.intersection(&second_half).next().unwrap();
p1_sum += type_to_priority(shared_type);
}
println!("P1: {}, P2: {}", p1_sum, p2_sum);
}
fn type_to_priority(ptype: &char) -> usize {
let mut ascii_val = *ptype as usize;
ascii_val -= LOWERCASE_ASCII_OFFSET;
if ascii_val > UPPERCASE_ASCII_OFFSET {
ascii_val -= UPPERCASE_ASCII_OFFSET
} else {
ascii_val += LOWERCASE_ALPHA_OFFSET
}
ascii_val
}