day4 done

This commit is contained in:
JasterV 2021-12-09 20:49:41 +01:00
parent f9ca3e5798
commit dbf7d357ed
4 changed files with 33 additions and 43 deletions

18
day4/Cargo.lock generated
View file

@ -5,21 +5,3 @@ version = 3
[[package]]
name = "day4"
version = "0.1.0"
dependencies = [
"itertools",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]

View file

@ -6,4 +6,3 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.10.3"

View file

@ -1,6 +1,4 @@
mod models;
use itertools::{FoldWhile::*, Itertools};
use models::Board;
use std::fs;
use std::io::Result;
@ -9,13 +7,13 @@ static INPUT_PATH: &str = "input.txt";
fn main() -> Result<()> {
let (nums, boards) = read_game(INPUT_PATH)?;
let (last_num, winning_board) = play_game(&nums, &boards);
let unmarked_cells_sum = winning_board
.iter()
.filter(|cell| !cell.is_marked())
.map(|cell| cell.into_inner())
.sum::<u16>();
println!("First puzzle: {}", last_num * unmarked_cells_sum);
let winners = play_game(&nums, &boards);
// First puzzle
let (winning_num, winning_board) = winners.first().unwrap();
println!("First puzzle: {}", winning_num * winning_board.get_score());
// Second puzzle
let (looser_num, looser_board) = winners.last().unwrap();
println!("Second puzzle: {}", looser_num * looser_board.get_score());
Ok(())
}
@ -30,16 +28,23 @@ fn read_game(filename: &str) -> Result<(Vec<u16>, Vec<Board>)> {
Ok((nums_to_draw, boards))
}
fn play_game(nums: &[u16], boards: &[Board]) -> (u16, Board) {
let (last_num, boards) = nums
fn play_game(nums: &[u16], boards: &[Board]) -> Vec<(u16, Board)> {
let (winning_boards, _) = nums
.iter()
.fold_while((0, Vec::from(boards)), |(last_num, boards), &num| {
if boards.iter().any(|board| board.has_won()) {
return Done((last_num, boards));
}
Continue((num, boards.iter().map(|board| board.mark(num)).collect()))
})
.into_inner();
let winning_board = boards.into_iter().find(|board| board.has_won()).unwrap();
(last_num, winning_board)
// Reduce nums list to a tuple of (winner_boards, remaining_boards)
// On each iteration we add the new winners to the winner_boards list
// and remove them from the remaining_boards list
.fold((vec![], Vec::from(boards)), |(winners, remaining), &num| {
let remaining = remaining.iter().map(|board| board.mark(num));
let round_winners = remaining
.clone()
.filter(|board| board.has_won())
.map(|board| (num, board));
let non_winners = remaining.into_iter().filter(|board| !board.has_won());
(
winners.into_iter().chain(round_winners).collect(),
non_winners.collect(),
)
});
winning_boards
}

View file

@ -42,6 +42,14 @@ impl Board {
Board { cells }
}
pub fn get_score(&self) -> u16 {
self.cells
.iter()
.filter(|cell| !cell.is_marked())
.map(|cell| cell.into_inner())
.sum::<u16>()
}
pub fn has_won(&self) -> bool {
(0..COLUMNS).any(|col| self.is_complete_column(col))
|| (0..ROWS).any(|row| self.is_complete_row(row))
@ -65,10 +73,6 @@ impl Board {
.map(|col| self.get(row, col))
.all(|cell| cell.map_or(false, |cell| cell.is_marked()))
}
pub fn iter(&self) -> std::slice::Iter<'_, Cell> {
self.cells.iter()
}
}
impl From<&str> for Board {