Advent of Code 2020

5.R 477B

12345678910111213141516
  1. library(stringr)
  2. input <- readLines("input5.txt")
  3. im <- str_match(input, "^([BF])([BF])([BF])([BF])([BF])([BF])([BF])([RL])([RL])([RL])$")
  4. rowspec <- apply(im[,2:8], c(1,2), function(x) {x == "B"})
  5. colspec <- apply(im[,9:11], c(1,2), function(x) {x == "R"})
  6. rownums <- as.vector(rowspec%*%(2^((ncol(rowspec)-1):0)))
  7. colnums <- as.vector(colspec%*%(2^((ncol(colspec)-1):0)))
  8. id <- rownums * 8 + colnums
  9. # Part a
  10. max(id)
  11. # Part b
  12. idl <- sort(id)
  13. idl[which.max(diff(idl))] + 1