diff --git a/2021/4/src/main.rs b/2021/4/src/main.rs index 9e43faf..81f0ccb 100644 --- a/2021/4/src/main.rs +++ b/2021/4/src/main.rs @@ -2,11 +2,6 @@ use std::fs; use std::collections::HashSet; fn main() { - part_one(); - part_two(); -} - -fn part_one() { let contents = fs::read_to_string("input") .expect("Something went wrong reading the file"); @@ -29,8 +24,7 @@ fn part_one() { let mut boards: Vec>> = groups.iter().map(|group| group .split('\n') .map(|x| x - .split(' ') - .filter(|x| *x != "") + .split_whitespace() .map(|x| x .parse::() .unwrap() @@ -40,73 +34,6 @@ fn part_one() { .collect() ).collect(); - for num in bingo_nums { - boards = boards - .into_iter() - .map(|board| board - .into_iter() - .map(|row| row - .into_iter() - .map(|x| if x == num { -1 } else { x }) - .collect() - ) - .collect() - ) - .collect(); - - let winning_boards: Vec<&Vec>> = boards.iter() - .filter(|board| - board.iter().filter(|row| - row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1 - ).collect::>>().len() > 0 || - [0, 1, 2, 3, 4].into_iter().filter(|column| - board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1 - ).collect::>().len() > 0 - ).collect(); - - if winning_boards.len() > 0 { - let sum: i32 = winning_boards.into_iter().take(1).flatten().flatten().filter(|x| **x != -1).sum(); - println!("Part 1, final score: {}", sum * num); - break; - } - } -} - -fn part_two() { - let contents = fs::read_to_string("input") - .expect("Something went wrong reading the file"); - - let mut groups: Vec = contents - .split("\n\n") - .map(|x| x - .to_string() - ) - .collect(); - - let bingo_nums: Vec = groups - .remove(0) - .split(',') - .map(|x| x - .parse::() - .unwrap() - ) - .collect(); - - let mut boards: Vec>> = groups.iter().map(|group| group - .split('\n') - .map(|x| x - .split(' ') - .filter(|x| *x != "") - .map(|x| x - .parse::() - .unwrap() - ) - .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![]; for num in bingo_nums { boards = boards @@ -122,21 +49,67 @@ fn part_two() { ) .collect(); - 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 || - [0, 1, 2, 3, 4].into_iter().filter(|column| - board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1 - ).collect::>().len() > 0 + let winning_boards: Vec<(usize, Vec>)> = boards + .iter() + .cloned() + .enumerate() + .filter(|(_, board)| + board + .iter() + .filter(|row| + row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1 + ) + .collect::>>() + .len() > 0 + || + (0..5) + .filter(|column| + board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1 + ).collect::>() + .len() > 0 ).collect(); + if winning_boards.len() == 1 { + let sum: i32 = winning_boards + .iter() + .map(|(_, x)| x) + .take(1) + .flatten().flatten() + .filter(|x| **x != -1) + .sum(); + + println!("Part 1, final score: {}", sum * num); + } + if winning_boards.len() == boards.len() { - 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 new_nums: HashSet = winning_boards + .iter() + .map(|(i, _)| i) + .cloned() + .collect(); + + let old_nums: HashSet = last_winning_boards + .iter() + .map(|(i, _)| 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(); + + 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; } @@ -144,3 +117,5 @@ fn part_two() { last_winning_boards = winning_boards; } } + +