2023 - Day 8: Haunted Wasteland

You’re still riding a camel across Desert Island when you spot a sandstorm quickly approaching. When you turn to warn the Elf, she disappears before your eyes! To be fair, she had just finished warning you about ghosts a few minutes ago.

One of the camel’s pouches is labeled “maps” - sure enough, it’s full of documents (your puzzle input) about how to navigate the desert. At least, you’re pretty sure that’s what they are; one of the documents contains a list of left/right instructions, and the rest of the documents seem to describe some kind of network of labeled nodes.

It seems like you’re meant to use the left/right instructions to navigate the network. Perhaps if you have the camel follow the same instructions, you can escape the haunted wasteland!

After examining the maps for a bit, two nodes stick out: AAA and ZZZ. You feel like AAA is where you are now, and you have to follow the left/right instructions until you reach ZZZ.

Set-Up

First day of needing to use scipen! Other than that, the data was in a nice enough structure to hard code the substrings for the columns.

Code
options(scipen = 999)
file_name <- "input.txt"
instructions <- readLines(file_name, n = 1L) |> strsplit("") |> unlist()

path_info <- readLines(file_name)[-1:-2]
paths <- data.frame(
  from = substr(path_info, 1, 3),
  L = substr(path_info, 8, 10),
  R = substr(path_info, 13, 15)
)
head(paths)
  from   L   R
1  QRX XNN TCJ
2  GSM PNH BVG
3  VDX VSG ZZZ
4  TGV SHD FVB
5  PPH MLX CBT
6  GRP FGH VLK

Part 1

Starting with AAA, you need to look up the next element based on the next left/right instruction in your input.

Of course, you might not find ZZZ right away. If you run out of left/right instructions, repeat the whole sequence of instructions as necessary: RL really means RLRLRLRLRLRLRLRL... and so on.

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

Created a small while loop to count the iterations, this was then functionalised for Part 2.

Code
calculate_niters <- function(path, x = "AAA", y = "ZZZ") {
  iter <- 0
  inst_i <- 1
  while (!grepl(y, x)) {
    x <- path[path$from == x, instructions[inst_i]]
    inst_i <- if (inst_i == length(instructions)) 1 else inst_i + 1
    iter <- iter + 1
  }
  iter
}

calculate_niters(paths)
[1] 20093

Part 2

The sandstorm is upon you and you aren’t any closer to escaping the wasteland. You had the camel follow the instructions, but you’ve barely left your starting position. It’s going to take significantly more steps to escape!

What if the map isn’t for people - what if the map is for ghosts? Are ghosts even bound by the laws of spacetime? Only one way to find out.

After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z! If you were a ghost, you’d probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z.

First read this as needed to convert everything ending in A as 1A, 2A etc (like the example). Was easier to create a function that used the code in part 1 when just needed to know when all ..A were in ..Z. Used the pracma::Lcm function to find the lowest common multiple because was quicker than writing something in base R!

Code
n_steps <- sapply(grep("..A", paths$from, value = TRUE), calculate_niters, path = paths, y = "..Z")
Reduce(pracma::Lcm, n_steps)
[1] 22103062509257