Advent of Code 2020 Day 17

Author

Nathan Moore

Write the Puzzle explanation here

# in this script, mainly just for pipe
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.2.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# this is the way I do things now
my_file <- here::here("2020", "data-2020-17.txt")

# read to find the width of the file
test <- read.delim(my_file, header = FALSE)

# width of the thing
width = length(test$V1)

# create vector with the widths to read
ww <- rep(1, width)

# this should work to create matrix like dataframe
# hash is not good becuase it is the default comment character
conway <- read.fwf(my_file, widths = ww, comment.char = "/") %>% 
  as.matrix()
Warning in readLines(file, n = thisblock): incomplete final line found on
'C:/Users/NathanMoore/code/advent-of-code/2020/data-2020-17.txt'

Maybe write an explanation of the solution approach here

# I'd like to convert to numeric to make summation easier
for (i in seq_len(width)) {
  for (j in seq_len(width)) {
    conway[i,j] = ifelse(conway[i,j] == "#", 1, 0)
  }
}

# this is an array to start with, with numeric values
start_conway = array(sapply(conway, as.numeric), dim = c(width, width, 1))

# this is the large array to end with
# don't want to build this incrementally, I don't think
# +2 to give us room for easy indexing
big_conway = array(0, dim = c(width + 2*6 + 2, 
                              width + 2*6 + 2, 
                              1 + 2*6 + 2))

# get the starting values into the middle of the big one
big_conway[8:15, 8:15, 8] <- start_conway
# big_conway[8:10, 8:10, 8] <- start_conway

checks

# check that numeric gives us the ability to sum sections
big_conway[ , , 8]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [2,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [3,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [4,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [5,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [8,]    0    0    0    0    0    0    0    0    1     0     1     1     0
 [9,]    0    0    0    0    0    0    0    0    0     0     0     1     0
[10,]    0    0    0    0    0    0    0    1    1     0     1     1     1
[11,]    0    0    0    0    0    0    0    0    1     0     1     0     1
[12,]    0    0    0    0    0    0    0    1    0     1     0     0     0
[13,]    0    0    0    0    0    0    0    0    1     0     0     1     1
[14,]    0    0    0    0    0    0    0    0    1     1     1     1     1
[15,]    0    0    0    0    0    0    0    1    0     0     1     1     1
[16,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[17,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[18,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[19,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[20,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[21,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[22,]    0    0    0    0    0    0    0    0    0     0     0     0     0
      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
 [1,]     0     0     0     0     0     0     0     0     0
 [2,]     0     0     0     0     0     0     0     0     0
 [3,]     0     0     0     0     0     0     0     0     0
 [4,]     0     0     0     0     0     0     0     0     0
 [5,]     0     0     0     0     0     0     0     0     0
 [6,]     0     0     0     0     0     0     0     0     0
 [7,]     0     0     0     0     0     0     0     0     0
 [8,]     0     1     0     0     0     0     0     0     0
 [9,]     1     1     0     0     0     0     0     0     0
[10,]     0     0     0     0     0     0     0     0     0
[11,]     1     1     0     0     0     0     0     0     0
[12,]     0     0     0     0     0     0     0     0     0
[13,]     1     0     0     0     0     0     0     0     0
[14,]     0     1     0     0     0     0     0     0     0
[15,]     1     0     0     0     0     0     0     0     0
[16,]     0     0     0     0     0     0     0     0     0
[17,]     0     0     0     0     0     0     0     0     0
[18,]     0     0     0     0     0     0     0     0     0
[19,]     0     0     0     0     0     0     0     0     0
[20,]     0     0     0     0     0     0     0     0     0
[21,]     0     0     0     0     0     0     0     0     0
[22,]     0     0     0     0     0     0     0     0     0
sum(big_conway[8:10, 8:10, 8])
[1] 3
i = 7
sum(big_conway[(i-1):(i+1), (i-1):(i+1), 8], na.rm = TRUE)
[1] 0

loop and answer

# loop through stages to find the expansion
for (s in seq_len(6)) {
  copy_conway <- big_conway
  for (x in seq(2, dim(big_conway)[1] - 1)){
    for (y in seq(2, dim(big_conway)[2] - 1)) {
      for (z in seq(2, dim(big_conway)[3] - 1)) {
        i_sum = sum(copy_conway[(x-1):(x+1), (y-1):(y+1), (z-1):(z+1)], na.rm = TRUE) - copy_conway[x, y, z]
        if (copy_conway[x, y, z] == 1) {
          if ((i_sum == 2 | i_sum == 3)) {
            # do nothing, stays active
          } else {
            big_conway[x, y, z] = 0
          }
        } else if (copy_conway[x, y, z] == 0 & i_sum == 3) {
          big_conway[x, y, z] = 1
        }
      }
    }
  }
}

# part one answer
sum(big_conway)
[1] 338

part two: four dimensions

# this is the large array to end with
# don't want to build this incrementally, I don't think
# +2 to give us room for easy indexing
d4_conway = array(0, dim = c(width + 2*6 + 2, 
                             width + 2*6 + 2, 
                             1 + 2*6 + 2, 
                             1 + 2*6 + 2))

# get the starting values into the middle of the big one
d4_conway[8:15, 8:15, 8, 8] <- start_conway
# d4_conway[8:10, 8:10, 8, 8] <- start_conway

d4_conway[ , , 8, 8]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [2,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [3,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [4,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [5,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0
 [8,]    0    0    0    0    0    0    0    0    1     0     1     1     0
 [9,]    0    0    0    0    0    0    0    0    0     0     0     1     0
[10,]    0    0    0    0    0    0    0    1    1     0     1     1     1
[11,]    0    0    0    0    0    0    0    0    1     0     1     0     1
[12,]    0    0    0    0    0    0    0    1    0     1     0     0     0
[13,]    0    0    0    0    0    0    0    0    1     0     0     1     1
[14,]    0    0    0    0    0    0    0    0    1     1     1     1     1
[15,]    0    0    0    0    0    0    0    1    0     0     1     1     1
[16,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[17,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[18,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[19,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[20,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[21,]    0    0    0    0    0    0    0    0    0     0     0     0     0
[22,]    0    0    0    0    0    0    0    0    0     0     0     0     0
      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
 [1,]     0     0     0     0     0     0     0     0     0
 [2,]     0     0     0     0     0     0     0     0     0
 [3,]     0     0     0     0     0     0     0     0     0
 [4,]     0     0     0     0     0     0     0     0     0
 [5,]     0     0     0     0     0     0     0     0     0
 [6,]     0     0     0     0     0     0     0     0     0
 [7,]     0     0     0     0     0     0     0     0     0
 [8,]     0     1     0     0     0     0     0     0     0
 [9,]     1     1     0     0     0     0     0     0     0
[10,]     0     0     0     0     0     0     0     0     0
[11,]     1     1     0     0     0     0     0     0     0
[12,]     0     0     0     0     0     0     0     0     0
[13,]     1     0     0     0     0     0     0     0     0
[14,]     0     1     0     0     0     0     0     0     0
[15,]     1     0     0     0     0     0     0     0     0
[16,]     0     0     0     0     0     0     0     0     0
[17,]     0     0     0     0     0     0     0     0     0
[18,]     0     0     0     0     0     0     0     0     0
[19,]     0     0     0     0     0     0     0     0     0
[20,]     0     0     0     0     0     0     0     0     0
[21,]     0     0     0     0     0     0     0     0     0
[22,]     0     0     0     0     0     0     0     0     0

loops

# loop through stages to find the expansion
# we could limit these sequences to a bounding box
for (s in seq_len(6)) {
  copy_conway <- d4_conway
  for (x in seq(2, dim(d4_conway)[1] - 1)){
    for (y in seq(2, dim(d4_conway)[2] - 1)) {
      for (z in seq(2, dim(d4_conway)[3] - 1)) {
        for (w in seq(2, dim(d4_conway)[4] - 1)) {
          # calculate sum for the surrounding cube
          i_sum = sum(copy_conway[(x-1):(x+1), 
                                  (y-1):(y+1), 
                                  (z-1):(z+1), 
                                  (w-1):(w+1)], na.rm = TRUE) - copy_conway[x, y, z, w]
          # if the cell is active
          if (copy_conway[x, y, z, w] == 1) {
            if ((i_sum == 2 | i_sum == 3)) {
              # do nothing, stays active
            } else {
              # too much or too little, make inactive
              d4_conway[x, y, z, w] = 0
            }
          } else if (copy_conway[x, y, z, w] == 0 & i_sum == 3) {
            # just the right conditions, becomes active
            d4_conway[x, y, z, w] = 1
          }
        }
      }
    }
  }
}

# part one answer
sum(d4_conway)
[1] 2440
# 1708 wrong