day03: Make it solve part 2 instead

This commit is contained in:
MasterofJOKers 2022-12-16 18:50:29 +01:00
parent 55038c87da
commit 6dfb583345
1 changed files with 11 additions and 5 deletions

View File

@ -15,13 +15,19 @@ fn main() -> Result<(), Box<dyn error::Error>> {
let reader = BufReader::new(f); let reader = BufReader::new(f);
let mut sum: u32 = 0; let mut sum: u32 = 0;
let mut group_items: HashSet<u32> = HashSet::new();
for line in reader.lines() { for (i, line) in reader.lines().enumerate() {
let line = line?; let line = line?;
let (c1, c2) = line.split_at(line.len() / 2); if group_items.is_empty() {
let c1_set = set_from_str(c1); group_items = set_from_str(line.as_str());
let c2_set = set_from_str(c2); } else {
sum += c1_set.intersection(&c2_set).sum::<u32>(); 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();
}
} }
println!("{}", sum); println!("{}", sum);