library(tidyverse)
drawn_numbers <- read_lines(file = "inputs/2021/04.txt", n_max = 1) |>
str_split_1(pattern = ",")
boards <- seq(from = 2, by = 6, length = 100) |>
map(\(skip) read_table(file = "inputs/2021/04.txt",
skip = skip,
n_max = 5,
col_types = "ccccc",
col_names = FALSE) |>
as.matrix() |>
unname())Day 4: Giant Squid
Part One
game_1 <- boards
board_mark <- function(board, drawn_number) {
board |>
str_replace(pattern = paste0("^", drawn_number, "$"),
replacement = "X") |>
matrix(ncol = 5)
}
full_rc <- function(board) {
indeces <- 1:5
target <- rep("X", times = 5)
# rows
rows <- indeces |>
map_lgl(\(r) all(board[r,] == target)) |>
any()
# rows
cols <- indeces |>
map_lgl(\(c) all(board[,c] == target)) |>
any()
return(rows | cols)
}
for (drawn_number in drawn_numbers) {
game_1 <- game_1 |>
map(\(board) board_mark(board, drawn_number))
check <- map_lgl(game_1, full_rc)
if (any(check)) {
index <- which(check == TRUE)
break
}
}
game_1[[index]] |>
str_remove_all("X") |>
as.integer() |>
sum(na.rm = TRUE) * as.integer(drawn_number)
## [1] 16716Part Two
game_2 <- boards
boards_indeces <- 1:100
for (drawn_number in drawn_numbers) {
game_2 <- game_2 |>
map(\(board) board_mark(board, drawn_number))
check <- map_lgl(game_2, full_rc)
if (any(check)) {
index <- which(check == TRUE)
boards_indeces <- setdiff(boards_indeces, index)
}
if (length(boards_indeces) == 1) {
last_winner <- boards_indeces
}
if (length(boards_indeces) == 0) {
break
}
}
game_2[[last_winner]] |>
str_remove_all("X") |>
as.integer() |>
sum(na.rm = TRUE) * as.integer(drawn_number)
## [1] 4880