Advent of Code 2023, Day 8

advent of code
Author

Michael Luu

Published

December 9, 2023

Part 1

This was definitely a fun puzzle. As always, lets start off with reading in the puzzle input, and tidy it up into something we can work with.

library(tidyverse)
library(here)

There’s two parts to the input, the first part is the instructions, and the second part are the nodes that can be traversed.

data <- read_lines(
  here('posts', 'aoc-2023-d8', 'puzzle-input.txt')
)

instructions <- data[[1]]
instructions <- instructions |> str_split('') 
instructions <- instructions[[1]]

data <- data[2:776] |> 
  as_tibble() |> 
  filter(value != '') |> 
  separate(value, into = c('id', 'direction'), sep = ' = ') |> 
  separate(direction, into = c('L', 'R'), sep = ', ') |> 
  mutate(L = str_remove(L, '\\(')) |> 
  mutate(R = str_remove(R, '\\)')) |> 
  arrange(id)

data <- data |> 
  pivot_longer(cols = c('L', 'R'), names_to = 'direction', values_to = 'value')

Now that we have the data into something we can work with let’s recap on the instructions. These are the set of instructions that needs to be traversed, e.g. the L or the R node in the data.

The following are a vector of instructions on whether to choose the L or the R for each of the nodes.

instructions
  [1] "L" "L" "R" "R" "R" "L" "L" "R" "L" "R" "R" "R" "L" "L" "R" "L" "R" "L"
 [19] "R" "L" "R" "L" "R" "R" "R" "L" "R" "R" "L" "R" "R" "L" "R" "L" "L" "L"
 [37] "R" "R" "L" "L" "R" "R" "L" "R" "R" "L" "R" "R" "L" "R" "R" "R" "L" "L"
 [55] "L" "R" "R" "L" "R" "L" "R" "R" "R" "L" "R" "R" "R" "L" "R" "L" "R" "R"
 [73] "L" "R" "R" "R" "L" "R" "L" "R" "R" "R" "L" "R" "L" "R" "L" "L" "L" "R"
 [91] "L" "R" "R" "L" "R" "L" "R" "R" "L" "R" "R" "R" "L" "R" "L" "R" "R" "R"
[109] "L" "R" "R" "R" "L" "R" "R" "R" "L" "R" "R" "R" "L" "R" "L" "R" "R" "R"
[127] "L" "R" "R" "R" "L" "R" "L" "L" "R" "R" "L" "R" "L" "R" "L" "R" "R" "R"
[145] "L" "R" "R" "L" "R" "R" "R" "L" "R" "R" "R" "L" "R" "R" "R" "L" "R" "R"
[163] "R" "L" "L" "L" "L" "R" "R" "L" "L" "R" "L" "R" "R" "L" "R" "R" "L" "R"
[181] "R" "R" "L" "R" "R" "R" "L" "L" "L" "R" "R" "L" "R" "R" "L" "R" "L" "R"
[199] "R" "L" "R" "R" "R" "L" "R" "R" "L" "R" "L" "R" "R" "R" "L" "R" "L" "R"
[217] "R" "L" "L" "R" "L" "L" "R" "R" "L" "R" "L" "R" "R" "R" "L" "R" "R" "L"
[235] "R" "R" "L" "R" "L" "R" "R" "L" "L" "L" "R" "R" "R" "L" "R" "L" "R" "R"
[253] "R" "L" "R" "L" "R" "L" "L" "R" "L" "R" "L" "R" "R" "R" "L" "R" "L" "R"
[271] "L" "R" "R" "R" "L" "R" "R" "L" "R" "R" "L" "R" "R" "R" "L" "R" "R" "L"
[289] "L" "R" "R" "R" "R"

Let’s also have a look at the nodes that can be traversed.

data
# A tibble: 1,548 × 3
   id    direction value
   <chr> <chr>     <chr>
 1 AAA   L         PBJ  
 2 AAA   R         RXK  
 3 BBA   L         TVV  
 4 BBA   R         VLF  
 5 BBJ   L         MCN  
 6 BBJ   R         RJD  
 7 BBN   L         FPQ  
 8 BBN   R         NFC  
 9 BBQ   L         MHL  
10 BBQ   R         VKQ  
# ℹ 1,538 more rows

The prompt of the puzzle is as follows.

Starting at AAA, follow the left/right instructions. How many steps are required to reach ZZZ?

We start off with writing a simple function that traces the path of the instructions. e.g. identify the node, and extract the value of the node.

trace_id_direction <- \(data, start_id, dir) {
  data |>
    filter(id == start_id) |>
    filter(direction == dir) |>
    pull(value)
  
}

Now we write a simple while loop to traverse the instructions. We start off at the node AAA, and then we trace the path of the instructions. We then update the node to the new node, and repeat the process until we reach the node ZZZ. The total number of iterations is the solution to the puzzle

i <- 1
position_id <- 'AAA'
results <- c()
while (position_id != 'ZZZ') {
  res <- map(instructions, \(x) {
    position_id <<- trace_id_direction(data, position_id, x)
    
    cat('position_id: ', position_id, ', iteration: ', i, '\n')
    i <<- i + 1
    
    return(position_id)
  }) |> unlist()
  
  results <- c(results, res)
  
}

Reuse

Citation

BibTeX citation:
@online{luu2023,
  author = {Luu, Michael},
  title = {Advent of {Code} 2023, {Day} 8},
  date = {2023-12-09},
  langid = {en}
}
For attribution, please cite this work as:
Luu, Michael. 2023. “Advent of Code 2023, Day 8.” December 9, 2023.