diff --git a/2021/4/src/main.rs b/2021/4/src/main.rs index 15791af..9e43faf 100644 --- a/2021/4/src/main.rs +++ b/2021/4/src/main.rs @@ -1,4 +1,5 @@ use std::fs; +use std::collections::HashSet; fn main() { part_one(); @@ -105,6 +106,8 @@ fn part_two() { .collect() ).collect(); + // Don't even ask okay, it works. I am a C programmer not a fp one :p + let mut last_winning_boards: Vec<(usize, Vec>)> = vec![]; for num in bingo_nums { boards = boards .into_iter() @@ -119,8 +122,8 @@ fn part_two() { ) .collect(); - let winning_boards: Vec<&Vec>> = boards.iter() - .filter(|board| + let winning_boards: Vec<(usize, Vec>)> = boards.iter().cloned().enumerate() + .filter(|(i, board)| board.iter().filter(|row| row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1 ).collect::>>().len() > 0 || @@ -130,9 +133,14 @@ fn part_two() { ).collect(); if winning_boards.len() == boards.len() { - let sum: i32 = winning_boards.into_iter().last().into_iter().flatten().flatten().filter(|x| **x != -1).sum(); - println!("Part 2, final score: {}", sum * num); + let new_nums: HashSet = winning_boards.iter().map(|(i, x)| i).cloned().collect(); + let old_nums: HashSet = last_winning_boards.iter().map(|(i, x)| i).cloned().collect(); + let that_one_fucker: HashSet = &new_nums - &old_nums; + let sum: i32 = winning_boards.get(that_one_fucker.iter().next().unwrap().clone()).unwrap().clone().1.into_iter().flatten().filter(|x| *x != -1).sum(); + println!("Part 2, final score: {:?}", sum * num); break; } + + last_winning_boards = winning_boards; } }