Day 23

Advent of Code: Worked Solutions

Puzzle Source
Puzzle Date

December 23, 2020

Setup

Import libraries:

library(tidyverse)

Read text input from file:

input <- read_lines("../input/day23.txt") |> 
  str_split_1("") |> 
  as.numeric()

Part 1

Define a game function which uses a linked list structure to quickly identify and reorder neighbors:

run_game <- function(init, n_moves) {
  
  # Convert ordered vector to linked list
  cur    <- head(init, 1)
  linked <- lead(init, default = cur)[order(init)]
  
  # For the specified number of rounds, reorder the linked list by game rules
  for (n in 1:n_moves) {
  
    clockwise <- cur
    pickup <- c()
    
    for (i in 1:3) {
      clockwise <- linked[clockwise]
      pickup <- c(pickup, clockwise)
    }
    
    dest <- setdiff(((cur - (1:4) - 1) %% length(linked)) + 1, pickup)[1]
  
    linked[cur]       <- linked[pickup[3]]
    linked[pickup[3]] <- linked[dest]
    linked[dest]      <- pickup[1]
  
    cur <- linked[cur]
  }
  
  linked
}

Run 100 rounds on the puzzle input:

output <- run_game(input, 100)

Gather the labels of all cups after cup 1, in order:

vec <- 1

for (n in 2:length(output)) {
  vec <- c(vec, output[tail(vec, 1)])
}

vec |> 
  tail(-1) |> 
  str_flatten("")

Part 2

Expand the puzzle input up to 1,000,000, then run 10,000,000 rounds on the new input:

output <- run_game(c(input, (max(input) + 1):1000000), 10000000)

Multiply the values of first two clockwise cups in the final output following cup 1:

output[1] * output[output[1]]