— Day 9: Rope Bridge —
Part 1
This rope bridge creaks as you walk along it. You aren’t sure how old it is, or whether it can even support your weight.
It seems to support the Elves just fine, though. The bridge spans a gorge which was carved out by the massive river far below you.
You step carefully; as you do, the ropes stretch and twist. You decide to distract yourself by modeling rope physics; maybe you can even figure out where not to step.
Consider a rope with a knot at each end; these knots mark the head and the tail of the rope. If the head moves far enough away from the tail, the tail is pulled toward the head.
Due to nebulous reasoning involving Planck lengths, you should be able to model the positions of the knots on a two-dimensional grid. Then, by following a hypothetical series of motions (your puzzle input) for the head, you can determine how the tail will move.
Due to the aforementioned Planck lengths, the rope must be quite short; in fact, the head (H) and tail (T) must always be touching (diagonally adjacent and even overlapping both count as touching).
If the head is ever two steps directly up, down, left, or right from the tail, the tail must also move one step in that direction so it remains close enough.
Otherwise, if the head and tail aren’t touching and aren’t in the same row or column, the tail always moves one step diagonally to keep up.
You just need to work out where the tail goes as the head follows a series of motions. Assume the head and the tail both start at the same position, overlapping.
After simulating the rope, you can count up all of the positions the tail visited at least once.
Read in our puzzle input:
day <- 9
input <- here::here(
"2022", "input",
paste0("day_", stringr::str_pad(day, 2, "left", "0"))
) |>
readLines()
test_input <- c(
"R 4",
"U 4",
"L 3",
"D 1",
"R 4",
"D 1",
"L 5",
"R 2"
)
move_h <- function(init_pos, step_dir) {
pos <- init_pos
if (step_dir == "R") pos[["x"]] <- (init_pos[["x"]] + 1)
else if (step_dir == "L") pos[["x"]] <- (init_pos[["x"]] - 1)
else if (step_dir == "D") pos[["y"]] <- (init_pos[["y"]] + 1)
else if (step_dir == "U") pos[["y"]] <- (init_pos[["y"]] - 1)
else stop("step_dir character not one of R, L, D or U.")
pos
}
move_t <- function(h_pos, t_pos) {
# to left, move right
if (t_pos[["x"]] < (h_pos[["x"]] - 1) & t_pos[["y"]] == h_pos[["y"]]) {
t_pos + c(1, 0)
# to right, move left
} else if (t_pos[["x"]] > (h_pos[["x"]] + 1) & t_pos[["y"]] == h_pos[["y"]]) {
t_pos + c(-1, 0)
# above, move down (y increases)
} else if (t_pos[["y"]] < (h_pos[["y"]] - 1) & t_pos[["x"]] == h_pos[["x"]]) {
t_pos + c(0, 1)
# below, move up (y decreases)
} else if (t_pos[["y"]] > (h_pos[["y"]] + 1) & t_pos[["x"]] == h_pos[["x"]]) {
t_pos + c(0, -1)
# diagonals
# left and above goes to left hand side of H
} else if (t_pos[["x"]] < (h_pos[["x"]] - 1) & t_pos[["y"]] < h_pos[["y"]]) {
t_pos + 1
# right and above goes to right hand side of H
} else if (t_pos[["x"]] > (h_pos[["x"]] + 1) & t_pos[["y"]] < h_pos[["y"]]) {
t_pos + c(-1, 1)
# left and above goes to top side of H
} else if (t_pos[["y"]] < (h_pos[["y"]] - 1) & t_pos[["x"]] < h_pos[["x"]]) {
t_pos + 1
# left and below goes to bottom side of H
} else if (t_pos[["y"]] > (h_pos[["y"]] + 1) & t_pos[["x"]] < h_pos[["x"]]) {
t_pos + c(1, -1)
# left and below goes to left hand side of H
} else if (t_pos[["x"]] < (h_pos[["x"]] - 1) & t_pos[["y"]] > h_pos[["y"]]) {
t_pos + c(1, -1)
# right and below goes to right hand side of H
} else if (t_pos[["x"]] > (h_pos[["x"]] + 1) & t_pos[["y"]] > h_pos[["y"]]) {
t_pos - 1
# right and above goes to top side of H
} else if (t_pos[["y"]] < (h_pos[["y"]] - 1) & t_pos[["x"]] > h_pos[["x"]]) {
t_pos + c(-1, 1)
# right and below goes to bottom side of H
} else if (t_pos[["y"]] > (h_pos[["y"]] + 1) & t_pos[["x"]] > h_pos[["x"]]) {
t_pos - 1
# don't move if no need to move
} else t_pos
}
process_step <- function(data_list, instr) {
h_pos <- data_list[[1]]
t_pos <- data_list[[2]]
visited <- data_list[[3]]
new_h_pos <- move_h(h_pos, instr)
new_t_pos <- move_t(new_h_pos, t_pos)
list(new_h_pos, new_t_pos, rbind(visited, new_t_pos))
}
expand_code <- function(x) {
x <- strsplit(x, " ") |> unlist()
rep(x[[1]], times = as.numeric(x[[2]]))
}
instructions <- input |>
purrr::map(expand_code) |>
purrr::flatten_chr()
h_pos <- c(x = 0, y = 0)
t_pos <- h_pos
out <- purrr::reduce(instructions, process_step, .init = list(h_pos, t_pos, t_pos))
tibble::as_tibble(out[[3]]) |>
dplyr::distinct() |>
nrow()
## [1] 5683
— Part Two —
A rope snaps! Suddenly, the river is getting a lot closer than you remember. The bridge is still there, but some of the ropes that broke are now whipping toward you as you fall through the air!
The ropes are moving too quickly to grab; you only have a few seconds to choose how to arch your body to avoid being hit. Fortunately, your simulation can be extended to support longer ropes.
Rather than two knots, you now must simulate a rope consisting of ten knots. One knot is still the head of the rope and moves according to the series of motions. Each knot further down the rope follows the knot in front of it using the same rules as before.
# work with rows of positions of previous knot, rather than instructions
process_step2 <- function(data_list, knot_in_front_pos) {
this_knot_pos <- data_list[[1]]
visited <- data_list[[2]]
new_knot_pos <- move_t(knot_in_front_pos, this_knot_pos)
list(new_knot_pos, dplyr::bind_rows(visited, new_knot_pos))
}
init_pos <- c(x = 0, y = 0)
out1 <- purrr::reduce(instructions, process_step, .init = list(init_pos, init_pos, init_pos))
knot_1_positions <- tibble::as_tibble(out1[[3]])
get_recursive <- function(positions_tbl, knot) {
init_pos <- c(x = 0, y = 0)
message(glue::glue("calculating positions for knot {knot}"))
prev_knot_positions <- positions_tbl |>
dplyr::rowwise() |>
dplyr::group_split()
out <- purrr::reduce(prev_knot_positions, process_step2, .init = list(init_pos, init_pos))
# while (knot < 5) {
# get_recursive(out[[2]], knot + 1)
# }
out[[2]]
}
knot_2 <- get_recursive(knot_1_positions, 2)
## calculating positions for knot 2
knot_2 |>
dplyr::distinct() |>
nrow()
## [1] 5045
knot_3 <- get_recursive(knot_2, 3)
## calculating positions for knot 3
knot_3 |>
dplyr::distinct() |>
nrow()
## [1] 4461
knot_4 <- get_recursive(knot_3, 4)
## calculating positions for knot 4
knot_4 |>
dplyr::distinct() |>
nrow()
## [1] 3956
knot_5 <- get_recursive(knot_4, 5)
## calculating positions for knot 5
knot_5 |>
dplyr::distinct() |>
nrow()
## [1] 3511
knot_6 <- get_recursive(knot_5, 6)
## calculating positions for knot 6
knot_6 |>
dplyr::distinct() |>
nrow()
## [1] 3171
knot_7 <- get_recursive(knot_6, 7)
## calculating positions for knot 7
knot_7 |>
dplyr::distinct() |>
nrow()
## [1] 2842
knot_8 <- get_recursive(knot_7, 8)
## calculating positions for knot 8
knot_8 |>
dplyr::distinct() |>
nrow()
## [1] 2584
knot_9 <- get_recursive(knot_8, 9)
## calculating positions for knot 9
knot_9 |>
dplyr::distinct() |>
nrow()
## [1] 2372
Based on a template by rongying.