2018-06: Chronal Coordinates

library(data.table)
library(dbscan)

Attaching package: 'dbscan'
The following object is masked from 'package:stats':

    as.dendrogram
dt <- fread("input.txt")
colnames(dt) <- c("x", "y")
dt$x <- dt$x + 1
dt$y <- dt$y + 1
dt$id <- 1:nrow(dt)
head(dt)
     x   y id
1: 109 325  1
2:  47  92  2
3: 357 217  3
4: 210 170  4
5: 171 332  5
6: 333 216  6

Part 1

size <- max(max(dt$x), max(dt$y))
mat <- matrix(NA, size, size)
mat[as.matrix(dt[, 1:2])] <- dt$id

for(x in 1:ncol(mat)) {
  for(y in 1:nrow(mat)) {
    distX <- abs(x - dt$x)
    distY <- abs(y - dt$y)
    dist <- (distX + distY) / 2
    closest <- dist[dist == min(dist)]
    mat[x,y] <- ifelse(length(closest) == 1, which.min(dist), NA)
  }
}

edges <- unique(c(mat[1, ], mat[,1], mat[nrow(mat), ], mat[, ncol(mat)]))
mat[mat %in% edges] <- NA
max(table(mat))
[1] 4166

Part 2

grid <- expand.grid(1:size, 1:size)
sapply(1:nrow(grid), function(i) {
  dist <- abs(dt$x - grid$Var1[i]) + abs(dt$y - grid$Var2[i])
  if(sum(dist) < 10000) { return(i) }
  else { return(NA) }
}) |> 
  na.exclude() |>
  length()
[1] 42250