# Libraries
library(tidyverse)
# Read input from file
path <- "../input/day05.txt"
input <- read_lines(path)Day 5
Advent of Code: Worked Solutions
Setup
Part 1
# Format raw input
moves <- input |>
tail_while(~ .x != "") |>
str_extract_all("\\d+") |>
map(as.integer)
stacks <- read_fwf(
path,
n_max = length(input) - length(moves) - 2,
col_types = "c"
) |>
mutate(across(everything(), ~ str_extract(.x, "[A-Z]"))) |>
as.list() |>
map(discard, is.na) |>
map(rev)
move_crates <- function(moves, stacks, func) {
# Execute moves
for (curr_move in moves) {
count <- curr_move[1]
from <- curr_move[2]
to <- curr_move[3]
crates <- func(tail(stacks[[from]], count))
stacks[[to]] <- append(stacks[[to]], crates)
stacks[[from]] <- head(stacks[[from]], -1 * count)
}
# Examine final top row of crates
stacks |>
map(~ tail(.x, 1)) |>
str_c(collapse = "")
}move_crates(moves, stacks, rev)Part 2
move_crates(moves, stacks, identity)