Printing Department
Part 1
This is one of those ones where you do great on the test input and miss something on the real one and can’t figure it out. I was returning too early in a few edge cases. Literal edge cases. Where the toilet paper was on the edge of the grid.
mat <- mistlecode::read_matrix('input.txt')
expand.grid(
'y' = mat |> nrow() |> seq_len(),
'x' = mat |> ncol() |> seq_len()
) |>
purrr::pmap(\(y, x) {
if (mat[y, x] == '.') return(c('.' = 4))
mistlecode::get_adjacent_values(y, x, mat) |>
as.vector() |>
table()
}) |>
unlist() |>
(\(x) x[names(x) == '@'])() |>
(\(x) x[x <= 4])() |>
length()[1] 1516
Part 2
A little bit more of me being not so bright on the execution and some waiting and there’s a solution.
mat <- mistlecode::read_matrix('input.txt')
wiped <- 0
coords <-
expand.grid(
'y' = mat |> nrow() |> seq_len(),
'x' = mat |> ncol() |> seq_len()
)
do_wipe <- function(mat) {
current <- wiped
mat_copy <- mat
coords |>
purrr::pwalk(\(y, x) {
if (mat[y, x] == '@') {
vals <-
mistlecode::get_adjacent_values(y, x, mat) |>
as.vector() |>
table()
if (vals['@'] <= 4) {
mat_copy[y, x] <<- '.'
wiped <<- wiped + 1
}
}
})
if (current != wiped) do_wipe(mat_copy) else wiped
}
do_wipe(mat)[1] 9122