Day 5

Author

Julia Romanowska

Data

The input is one file, but two parts:

  • first, the order of crates in stacks
  • next, the movement procedure
input_file_path <- here("DATA", "2022", "input_day_05.txt")

cat(system(paste("head", input_file_path), intern = TRUE), sep = "\n")
[N]     [Q]         [N]            
[R]     [F] [Q]     [G] [M]        
[J]     [Z] [T]     [R] [H] [J]    
[T] [H] [G] [R]     [B] [N] [T]    
[Z] [J] [J] [G] [F] [Z] [S] [M]    
[B] [N] [N] [N] [Q] [W] [L] [Q] [S]
[D] [S] [R] [V] [T] [C] [C] [N] [G]
[F] [R] [C] [F] [L] [Q] [F] [D] [P]
 1   2   3   4   5   6   7   8   9 

I should probably create a nice code where I read in the input and convert it to the list of vectors, but I don’t have time now 😎

I have splitted the input file to get separate input for the stacks and for the instructions.

system("./Day_5_split_input.sh")
list.files(here("DATA"))
[1] "2022" "2023"
(instructions <- read_table(
  here("DATA", "2022", "input_day_05_instructions.txt"),
  show_col_types = FALSE,
  col_names = FALSE
))
# A tibble: 503 × 6
   X1       X2 X3       X4 X5       X6
   <chr> <dbl> <chr> <dbl> <chr> <dbl>
 1 move      3 from      9 to        4
 2 move      2 from      5 to        2
 3 move      8 from      1 to        9
 4 move      4 from      7 to        1
 5 move      5 from      3 to        8
 6 move      3 from      3 to        7
 7 move     11 from      8 to        3
 8 move      7 from      3 to        6
 9 move      2 from      5 to        9
10 move      3 from      1 to        6
# ℹ 493 more rows
instructions <- instructions %>%
  select(move_how_many = X2, from = X4, to = X6)

Now, creating a list of vectors for the initial stack of crates.

init_stacks <- list(
  stack1 = c("F", "D", "B", "Z", "T", "J", "R", "N"),
  stack2 = c("R", "S", "N", "J", "H"),
  stack3 = c("C", "R", "N", "J", "G", "Z", "F", "Q"),
  stack4 = c("F", "V", "N", "G", "R", "T", "Q"),
  stack5 = c("L", "T", "Q", "F"),
  stack6 = c("Q", "C", "W", "Z", "B", "R", "G", "N"),
  stack7 = c("F", "C", "L", "S", "N", "H", "M"),
  stack8 = c("D", "N", "Q", "M", "T", "J"),
  stack9 = c("P", "G", "S")
)

Functions

This looks like we need some type queue function:

push_back <- function(stacks, from, to){
  stack_name_from <- paste0("stack", from)
  stack_name_to <- paste0("stack", to)
  
  length_from <- length(stacks[[stack_name_from]])
  
  crate_to_move <- stacks[[stack_name_from]][length_from]
  new_stacks <- stacks
  new_stacks[[stack_name_from]] <- stacks[[stack_name_from]][-length_from]
  new_stacks[[stack_name_to]] <- c(stacks[[stack_name_to]], crate_to_move)
  
  return(new_stacks)
}

Rearranging

This is best in base R…

cur_stacks <- init_stacks

for(i in nrow(instructions)){
  cur_instruction <- instructions[i,]

  for(j in 1:(cur_instruction$move_how_many)){
    cur_stacks <<- push_back(cur_stacks, cur_instruction$from, cur_instruction$to)
  }
}
cur_stacks
$stack1
[1] "F" "D" "B" "Z" "T" "J" "R" "N"

$stack2
[1] "R" "S" "N" "J" "H"

$stack3
[1] "C" "R" "N" "J" "G" "Z" "F" "Q"

$stack4
[1] "F" "V" "N" "G" "R" "T" "Q"

$stack5
[1] "L" "T" "Q" "F"

$stack6
[1] "Q" "C" "W" "Z" "B"

$stack7
[1] "F" "C" "L" "S" "N" "H" "M"

$stack8
[1] "D" "N" "Q" "M" "T" "J"

$stack9
[1] "P" "G" "S" "N" "G" "R"

What’s on top?

top_crates <- map_chr(
  cur_stacks,
  function(stack){
    l_stack <- length(stack)
    return(stack[l_stack])
  }
)

The last crates on all stacks: NHQQFBMJR! Wrong? 🤔