2023

— R Day 8 Part 1 —

solve_day8_part1 <- function(input) {
 instructions <- unlist(strsplit(input[[1]], ""))

 nodes <- input[-c(1, 2)]
 nodes <- stringi::stri_extract_all(nodes, regex = "[A-Z]{3}")

 names(nodes) <- vapply(nodes, \(x) x[[1]], character(1))

 nodes <- lapply(nodes, function(x) {
   x <- x[-1]
   names(x) <- c("L", "R")
   x
 })

 # starting state
 current_node <- "AAA"
 steps <- 0
 instruction <- 1

 while (current_node != "ZZZ") {
   # get nodes current nodes point to
   current_node <- nodes[[current_node]]
   # follow step instruction to next node
   current_node <- current_node[[instructions[[instruction]]]]

   steps <- steps + 1

   # next instruction or repeat instructions after going through them all
   if (instruction < length(instructions)) {
     instruction <- instruction + 1
   } else {
     instruction <- 1
   }
 }

 steps
}
Run
aoc_source(day = 8, part = 1)

input = aoc_read(day = 8)

aoc_run(solve_day8_part1(input))
Elapsed: 0.037 seconds
Memory:  610 KB

— R Day 8 Part 2 —

solve_day8_part2 <- function(input) {
 instructions <- unlist(strsplit(input[[1]], ""))

 nodes <- input[-c(1, 2)]
 nodes <- stringi::stri_extract_all(nodes, regex = "[A-Z]{3}")

 names(nodes) <- vapply(nodes, \(x) x[[1]], character(1))

 nodes <- lapply(nodes, function(x) {
   x <- x[-1]
   names(x) <- c("L", "R")
   x
 })

 # starting state
 current_nodes <- names(nodes)[endsWith(names(nodes), "A")]
 steps <- rep(0, length(current_nodes))
 instruction <- 1

 # end state
 while (!all(endsWith(current_nodes, "Z"))) {
   # ASSUMING start-end nodes come in 1:1 pairs
   # loop through each node's sequence until it first encounters its end node
   # storing the number of steps
   for (node_i in seq_along(current_nodes)) {
     if (!endsWith(current_nodes[[node_i]], "Z")) {
       # get nodes current nodes point to
       full_node <- nodes[[current_nodes[[node_i]]]]
       # follow step instruction to next node
       current_nodes[[node_i]] <- full_node[[instructions[[instruction]]]]

       steps[[node_i]] <- steps[[node_i]] + 1
     }
   }

   # next instruction or repeat instructions after going through them all
   if (instruction < length(instructions)) {
     instruction <- instruction + 1
   } else {
     instruction <- 1
   }
 }

 # ASSUMING each start-end pair leads to an end-end cycle
 # of the same number of steps
 # find the smallest common multiple of the steps
 # i.e. all cycles align
 cheapr::scm(steps)
}
Run
aoc_source(day = 8, part = 2)

input = aoc_read(day = 8)

aoc_run(solve_day8_part2(input))
Elapsed: 0.384 seconds
Memory:  447 KB
Back to top