# Libraries
library(tidyverse)
# Read input from file
<- read_lines("../input/day11.txt", skip_empty_rows = TRUE) input
Day 11
Advent of Code: Worked Solutions
Setup
Part 1
Convert text input to a matrix:
<- input |>
mtx str_split("") |>
map(partial(matrix, nrow = 1)) |>
reduce(rbind)
Determine the initial indices of each galaxy:
<- which(mtx == '#', arr.ind = TRUE) |>
galaxies as_tibble() |>
mutate(id = row_number(), .before = everything())
Get the indices of the rows and columns that contain only empty space:
<- split(mtx, row(mtx)) |>
empty_rows map_lgl(~ all(.x == '.')) |>
which() |>
unname()
<- split(mtx, col(mtx)) |>
empty_cols map_lgl(~ all(.x == '.')) |>
which() |>
unname()
Adjust the coordinates of the galaxies by doubling the empty rows and columns:
<- galaxies |>
galaxies mutate(
empty_pre_rows = map_int(row, ~ sum(empty_rows < .x)),
empty_pre_cols = map_int(col, ~ sum(empty_cols < .x))
)
Define a function that adjusts the coordinates of each galaxy by expanding all empty rows and columns by n, computes the manhattan distance between each resulting pair of galaxies, and sums the result:
<- function(n) {
expanded_galaxy_dist
<- galaxies |>
new_galaxies mutate(
new_row = row + empty_pre_rows * (n - 1),
new_col = col + empty_pre_cols * (n - 1)
)
left_join(
select(new_galaxies, id, row = new_row, col = new_col),
select(new_galaxies, id, row = new_row, col = new_col),
join_by(x$id < y$id)
|>
) mutate(dist = abs(row.x - row.y) + abs(col.x - col.y)) |>
pull(dist) |>
sum(na.rm = TRUE)
}
Run on puzzle input:
expanded_galaxy_dist(2)
Part 2
expanded_galaxy_dist(1000000)