2021 Day 11

Author

Nathan Moore

— Day 11: Dumbo Octopus —

Watch our for the bioluminescent octopuses.

Given the starting energy levels of the dumbo octopuses in your cavern, simulate 100 steps. How many total flashes are there after 100 steps?

library(tidyverse)

my_file <- here::here("2021", "data-2021-11.txt")
x <- readLines(my_file)

Function!

increment_octo <- function(arr, vct) {
  for (j in seq(2, 11)) {
    for (k in seq(2, 11)) {
      pos = (k - 1) * 12 + j
      if (arr[j, k] > 9 & pos %in% vct) {
        # print(pos)
        arr[(j-1):(j+1), (k-1):(k+1)] = arr[(j-1):(j+1), (k-1):(k+1)] + 1
      }
    }
  }
  return(arr)
}

Loops!

# array, I think
octo_s = stringr::str_split_fixed(x, "", 10) %>% as.data.frame()
octo_s = sapply(octo_s, as.numeric)

octo = array(NA, c(12, 12))
octo[2:11, 2:11] = octo_s
# keep track of how many flashes there have been
counter = 0

# loop through 100 times
# is part two going to be a million times?
for (j in seq(100)) {
    # print(paste("iteration: ", j))
    # add one to the array
    octo = octo + 1
    # are any octopi going to flash?
    flash = which(octo > 9)
    # if none flash, go to next iteration
    if (!any(flash, na.rm = TRUE)) next

    # keep track of which have already flashed
    new_flash = vector()
    old_flash = vector()
    # if we do have some flashing
    repeat {
        # new flash, iterate these ones
        new_flash = setdiff(flash, old_flash)
        # allocate to keep track
        old_flash = flash
        # this function takes care of iteration
        octo = increment_octo(octo, new_flash)
        # get a new list of flashers
        flash = which(octo > 9)
        # break the repeat if we aren't flashing any more
        # length might not be the best test but we can only add so it's ok?
        if (length(flash) == length(old_flash)) break
    }
    octo[octo > 9] = 0
    counter = counter + length(flash)
    # print(paste("length of flashes: ", length(flash)))

}

counter 
[1] 1617

— Part Two —

If you can calculate the exact moments when the octopuses will all flash simultaneously, you should be able to navigate through the cavern. What is the first step during which all octopuses flash?

# array, I think
octo_s = stringr::str_split_fixed(x, "", 10) %>% as.data.frame()
octo_s = sapply(octo_s, as.numeric)

octo = array(NA, c(12, 12))
octo[2:11, 2:11] = octo_s
# keep track of how many flashes there have been
counter = 0

# loop through 100 times
for (j in seq(1000)) {
    # print(paste("iteration: ", j))
    # add one to the array
    octo = octo + 1
    # are any octopi going to flash?
    flash = which(octo > 9)
    # if none flash, go to next iteration
    if (!any(flash, na.rm = TRUE)) next

    # keep track of which have already flashed
    new_flash = vector()
    old_flash = vector()
    # if we do have some flashing
    repeat {
        # new flash, iterate these ones
        new_flash = setdiff(flash, old_flash)
        # allocate to keep track
        old_flash = flash
        # this function takes care of iteration
        octo = increment_octo(octo, new_flash)
        # get a new list of flashers
        flash = which(octo > 9)
        # break the repeat if we aren't flashing any more
        # length might not be the best test but we can only add so it's ok?
        if (length(flash) == length(old_flash)) break
    }
    octo[octo > 9] = 0
    counter = counter + length(flash)
    # print(paste("length of flashes: ", length(flash)))
    if (length(flash) == 100) break
}

counter
[1] 4026
j
[1] 258