2022-12-16 18:33:33 +01:00
|
|
|
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;
|
2022-12-16 18:50:29 +01:00
|
|
|
let mut group_items: HashSet<u32> = HashSet::new();
|
2022-12-16 18:33:33 +01:00
|
|
|
|
2022-12-16 18:50:29 +01:00
|
|
|
for (i, line) in reader.lines().enumerate() {
|
2022-12-16 18:33:33 +01:00
|
|
|
let line = line?;
|
2022-12-16 18:50:29 +01:00
|
|
|
if group_items.is_empty() {
|
|
|
|
group_items = set_from_str(line.as_str());
|
|
|
|
} else {
|
|
|
|
group_items = set_from_str(line.as_str()).intersection(&group_items).cloned().collect();
|
|
|
|
}
|
|
|
|
if (i + 1) % 3 == 0 {
|
|
|
|
sum += group_items.iter().next().unwrap();
|
|
|
|
group_items.drain();
|
|
|
|
}
|
2022-12-16 18:33:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("{}", sum);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|