Advertisement · 728 × 90
#
Hashtag
#AoCDay4
Advertisement · 728 × 90
# Day 4 - Printing Department - Part 1

D4mat <- do.call(rbind, strsplit(readLines("D4input.txt"), ""))  #load the data into a matrix 
matdim <- nrow(D4mat)

D4mat <- pad_matrix(D4mat)


# then just loop through every cell from [2,2] to [matdim+1,matdim+1] and count up the @s in all 8 directions
rollcount <- 0
for (i in 2:(matdim+1)) {
  for (j in 2:(matdim+1)) {
    if(D4mat[i,j] == "@") {
      neighbours <- sum(D4mat[i-1,j-1] == "@",D4mat[i-1,j] == "@", D4mat[i-1,j+1] == "@",
                        D4mat[i,j-1] == "@", F, D4mat[i,j+1] == "@",
                        D4mat[i+1,j-1] == "@",D4mat[i+1,j] == "@", D4mat[i+1,j+1] == "@")
      if(neighbours < 4) {rollcount <- rollcount + 1} }}}

print(paste("answer is",rollcount))

# Day 4 - Printing Department - Part 1 D4mat <- do.call(rbind, strsplit(readLines("D4input.txt"), "")) #load the data into a matrix matdim <- nrow(D4mat) D4mat <- pad_matrix(D4mat) # then just loop through every cell from [2,2] to [matdim+1,matdim+1] and count up the @s in all 8 directions rollcount <- 0 for (i in 2:(matdim+1)) { for (j in 2:(matdim+1)) { if(D4mat[i,j] == "@") { neighbours <- sum(D4mat[i-1,j-1] == "@",D4mat[i-1,j] == "@", D4mat[i-1,j+1] == "@", D4mat[i,j-1] == "@", F, D4mat[i,j+1] == "@", D4mat[i+1,j-1] == "@",D4mat[i+1,j] == "@", D4mat[i+1,j+1] == "@") if(neighbours < 4) {rollcount <- rollcount + 1} }}} print(paste("answer is",rollcount))

#function to pad the matrix out with full stops
pad_matrix <- function(mat, pad = ".", n = 1) {
  nr <- nrow(mat)
  nc <- ncol(mat)
  top_bottom <- matrix(pad, nrow = n, ncol = nc + 2 * n)
  side <- matrix(pad, nrow = nr, ncol = n)
  rbind(top_bottom, cbind(side, mat, side), top_bottom)
}

#function to pad the matrix out with full stops pad_matrix <- function(mat, pad = ".", n = 1) { nr <- nrow(mat) nc <- ncol(mat) top_bottom <- matrix(pad, nrow = n, ncol = nc + 2 * n) side <- matrix(pad, nrow = nr, ncol = n) rbind(top_bottom, cbind(side, mat, side), top_bottom) }

#Part 2 - use the same padded matrix that was set up in part 1
#instead of just counting the reachable rolls, this time count them and also replace with a dot to 'remove' them.  
# keep passing through until no more rolls get removed.

rollcount <- 0

while (TRUE) { 
  removed <- 0
  for (i in 2:(matdim+1)) {
    for (j in 2:(matdim+1)) {
      if(D4mat[i,j] == "@") {
        neighbours <- sum(D4mat[i-1,j-1] == "@",D4mat[i-1,j] == "@", D4mat[i-1,j+1] == "@",
                          D4mat[i,j-1] == "@", F, D4mat[i,j+1] == "@",
                          D4mat[i+1,j-1] == "@",D4mat[i+1,j] == "@", D4mat[i+1,j+1] == "@")
        if(neighbours < 4) {
          rollcount <- rollcount + 1
          removed <- removed + 1
          D4mat[i,j] <- '.'}}}}
  if (removed == 0) break }

print(paste("answer is",rollcount))

#Part 2 - use the same padded matrix that was set up in part 1 #instead of just counting the reachable rolls, this time count them and also replace with a dot to 'remove' them. # keep passing through until no more rolls get removed. rollcount <- 0 while (TRUE) { removed <- 0 for (i in 2:(matdim+1)) { for (j in 2:(matdim+1)) { if(D4mat[i,j] == "@") { neighbours <- sum(D4mat[i-1,j-1] == "@",D4mat[i-1,j] == "@", D4mat[i-1,j+1] == "@", D4mat[i,j-1] == "@", F, D4mat[i,j+1] == "@", D4mat[i+1,j-1] == "@",D4mat[i+1,j] == "@", D4mat[i+1,j+1] == "@") if(neighbours < 4) { rollcount <- rollcount + 1 removed <- removed + 1 D4mat[i,j] <- '.'}}}} if (removed == 0) break } print(paste("answer is",rollcount))

I was on an #AdventOfCode roll last night - completing both #Day4 and day5 (but I'll post day 5 tomorrow)

For once I had an evening free of commitments to work or family - so I could attend to my commitment to the elves :)

First matrix puzzle this year

#AoCDay4 #RStats

6 0 0 0