45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
|
use std::{
|
||
|
collections::HashSet,
|
||
|
io::{stdin, Read},
|
||
|
};
|
||
|
|
||
|
fn item_priority(item: char) -> u32 {
|
||
|
match item {
|
||
|
'a'..='z' => item as u32 - 'a' as u32 + 1,
|
||
|
'A'..='Z' => item as u32 - 'A' as u32 + 27,
|
||
|
_ => unreachable!(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let mut data = String::new();
|
||
|
stdin().read_to_string(&mut data).unwrap();
|
||
|
|
||
|
let mut sum = 0;
|
||
|
for line in data.lines() {
|
||
|
let (left, right) = line.split_at(line.len() / 2);
|
||
|
assert_eq!(left.len(), right.len());
|
||
|
|
||
|
let left: HashSet<_> = left.chars().collect();
|
||
|
let right: HashSet<_> = right.chars().collect();
|
||
|
|
||
|
let common = *left.intersection(&right).next().unwrap();
|
||
|
sum += item_priority(common);
|
||
|
}
|
||
|
|
||
|
println!("{}", sum);
|
||
|
|
||
|
let mut sum = 0;
|
||
|
for lines in data.lines().collect::<Vec<_>>().chunks(3) {
|
||
|
let common = lines
|
||
|
.iter()
|
||
|
.map(|line| line.chars().collect::<HashSet<_>>())
|
||
|
.reduce(|l, r| &l & &r)
|
||
|
.unwrap();
|
||
|
assert_eq!(common.len(), 1);
|
||
|
|
||
|
sum += item_priority(common.into_iter().next().unwrap());
|
||
|
}
|
||
|
println!("{}", sum);
|
||
|
}
|