Day 4 part 2 done in Rust!

This commit is contained in:
Steph 2022-12-01 21:02:00 +01:00
parent 93cab67a10
commit dc1725a103

View File

@ -1,4 +1,5 @@
use std::fs; use std::fs;
use std::collections::HashSet;
fn main() { fn main() {
part_one(); part_one();
@ -105,6 +106,8 @@ fn part_two() {
.collect() .collect()
).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<i32>>)> = vec![];
for num in bingo_nums { for num in bingo_nums {
boards = boards boards = boards
.into_iter() .into_iter()
@ -119,8 +122,8 @@ fn part_two() {
) )
.collect(); .collect();
let winning_boards: Vec<&Vec<Vec<i32>>> = boards.iter() let winning_boards: Vec<(usize, Vec<Vec<i32>>)> = boards.iter().cloned().enumerate()
.filter(|board| .filter(|(i, board)|
board.iter().filter(|row| board.iter().filter(|row|
row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1 row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1
).collect::<Vec<&Vec<i32>>>().len() > 0 || ).collect::<Vec<&Vec<i32>>>().len() > 0 ||
@ -130,9 +133,14 @@ fn part_two() {
).collect(); ).collect();
if winning_boards.len() == boards.len() { if winning_boards.len() == boards.len() {
let sum: i32 = winning_boards.into_iter().last().into_iter().flatten().flatten().filter(|x| **x != -1).sum(); let new_nums: HashSet<usize> = winning_boards.iter().map(|(i, x)| i).cloned().collect();
println!("Part 2, final score: {}", sum * num); let old_nums: HashSet<usize> = last_winning_boards.iter().map(|(i, x)| i).cloned().collect();
let that_one_fucker: HashSet<usize> = &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; break;
} }
last_winning_boards = winning_boards;
} }
} }