You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
668 B

use std::collections::HashSet;
use std::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
fn set_from_str(chars: &str) -> HashSet<u32> {
HashSet::from_iter(chars.chars().map(|c| if c.is_uppercase() { c as u32 - 38 } else { c as u32 - 96 }))
}
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut sum: u32 = 0;
for line in reader.lines() {
let line = line?;
let (c1, c2) = line.split_at(line.len() / 2);
let c1_set = set_from_str(c1);
let c2_set = set_from_str(c2);
sum += c1_set.intersection(&c2_set).sum::<u32>();
}
println!("{}", sum);
Ok(())
}