No Matter How You Slice It

Published

December 3, 2018

library(data.table)
library(stringi)
dt <- readLines("input.txt")
pattern <- 
  "#([0-9]{1,4}) @ ([0-9]{0,4}),([0-9]{0,4}): ([0-9]{0,4})x([0-9]{0,4})"
dt <- 
  data.table(do.call(rbind, stri_match_all_regex(dt, pattern)))
colnames(dt) <- c("input", "id", "xPos", "yPos", "xSize", "ySize")
dt <- data.table(apply(dt[, 2:6], 2, as.numeric))
head(dt)
      id  xPos  yPos xSize ySize
   <num> <num> <num> <num> <num>
1:     1   393   863    11    29
2:     2   675   133    15    26
3:     3   690   605    25    22
4:     4   342   752    19    17
5:     5   840    36    14    18
6:     6   671   653    10    23

Part 1

mat <- matrix(0, 1000, 1000)
for(x in 1:nrow(dt)) {
  xPos <- dt$xPos[x] + 1
  xSize <- dt$xSize[x] - 1
  yPos <- dt$yPos[x] + 1
  ySize <- dt$ySize[x] - 1
  mat[xPos:(xPos + xSize), yPos:(yPos + ySize)] <- 
    mat[xPos:(xPos + xSize), yPos:(yPos + ySize)] + 1
}
length(mat[mat >= 2])
[1] 98005

Part 2

for(x in 1:nrow(dt)) {
  xPos <- dt$xPos[x] + 1
  xSize <- dt$xSize[x] - 1
  yPos <- dt$yPos[x] + 1
  ySize <- dt$ySize[x] - 1
  if (all(mat[xPos:(xPos + xSize), yPos:(yPos + ySize)] == 1)) { print(x) }
}
[1] 331