AOC2022/rust/3/part1.rs

28 lines
917 B
Rust

use std::{
collections::{hash_map::RandomState, HashSet},
fs::File,
io::{BufRead, BufReader},
iter::FromIterator,
};
fn main() -> () {
let input = File::open("input.txt").unwrap();
let buf = BufReader::new(input);
let score: u32 = buf.lines().map(|line| {
let mut firsthalf = line.unwrap();
let secondhalf = firsthalf.split_off(firsthalf.len() / 2);
let set_a = HashSet::<_, RandomState>::from_iter(firsthalf.chars());
let set_b = HashSet::<_, RandomState>::from_iter(secondhalf.chars());
set_a.intersection(&set_b).map(|&char| {
let score: u32 = match char {
'A'..='Z' => char as u32 - 'A' as u32 + 27,
'a'..='z' => char as u32 - 'a' as u32 + 1,
_ => panic!("nigger, that's invalid")
};
score
}).sum::<u32>()
}).sum();
println!("{:?}", score);
}