library(stringr)
library(plyr)
#library(pdist) # apparently CompileBot doesn't have this package
source(url('https://p...content-available-to-author-only...n.com/raw/ekg9ZTMW')) # just to get pdist, lol
# Functions
formatrow <- function(s, NCOL) strsplit(str_pad(s, NCOL, side = 'right'), '')[[1]]
makemap <- function(mapdata, NCOL) adply(.data = mapdata, .margins = 1, .fun = formatrow, NCOL, .id = NULL)
centermap <- function(map, COL) {
NCOL <- ncol(map)
cols <- rep(seq(NCOL), 3)
indices <- cols[seq(COL + NCOL - center, COL + NCOL + center - 1)]
map[, indices]
}
findland <- function(map) which(map == '#', arr.ind = T)
findwater <- function(map) which(map == ' ', arr.ind = T)
getdistances <- function(map, COL) {
map <- centermap(map, COL)
water <- cbind(findwater(map[, 41]), 41)
land <- findland(map)
dists <- as.matrix(pdist(water, land))
apply(dists, 1, min)
}
getNemo <- function(map) {
dists <- unlist(sapply(seq(ncol(map)), getdistances, map = map))
water <- findwater(map)
water[which.max(dists), ]
}
drawMap <- function(map, point = NULL) {
if(!is.null(point))
map[point[1], point[2]] <- '*'
invisible(apply(map, 1, function(x) writeLines(paste0(x, collapse = ''))))
}
runIt <- function(mapdata, NCOL, NROW) {
map <- makemap(mapdata, NCOL)
nemo <- getNemo(map)
writeLines(paste('Row:', nemo[1], '\tCol:', nemo[2], '\n'))
drawMap(map, nemo)
}
# Input Data
NCOL <- 80
NROW <- 25
mapdata <- c(" ## # # # # # # ## ###",
" #### ###### ######## ###### ##### ######### #### #######",
" ########## ## ##### #### # #####################",
" ####################### ## ### ## #### #### ##",
" ######### ######### ### ## # ### ## ##",
"# # ##### ####### ### # #",
" # ### ## #######",
" # ### ########### #",
" ### ## ############## #",
"# ### ############## #",
" ## #############",
" ##### ########### ##",
" ######### ########## ##",
" ############ ######### ##",
" ############### #######",
" ############## ##### #########",
" ############### ## ### ###########",
" ############### # ############",
" ############ ### ####",
" ######### #",
"# #####",
"",
" ######## ###### #######",
" ###################### ########################### ##############",
"##############################################################################")
# Run it
runIt(mapdata, NCOL,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yAjIyMjICMjIyMjIyMiLAogICAgICAgICAgICAgIiAgICMjIyMjIyMjIyMgIyMgIyMjIyMgICAgIyMjIyAgICAjICAgICAgICAgICMjIyMjIyMjIyMjIyMjIyMjIyMjIyIsCiAgICAgICAgICAgICAiICAgICMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjICAgICAgIyMgICAgICAgICAgICAjIyMgIyMgICMjIyMgIyMjIyAgIyMiLAogICAgICAgICAgICAgIiAgICAgIyMjIyMjIyMjICMjIyMjIyMjIyAgICAgICAgICMjIyAgICAgICAgICAgICMjICAjICAgIyMjICMjICAgIyMiLAogICAgICAgICAgICAgIiMgICAgICMgIyMjIyMgICAjIyMjIyMjICAgICAgICAgIyMjICAgICAgICAgICAgICAgICAgICAgICMgICAgICAjIiwKICAgICAgICAgICAgICIgICAgICAjICAgIyMjICAgICAgICMjICAgICAgICAgICAgICAgICAgICAgICAgICAjIyMjIyMjIiwKICAgICAgICAgICAgICIgICAgICAjICAgICMjIyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMjIyMjIyMjIyMjICAgICAjIiwKICAgICAgICAgICAgICIgICAgICAgICAgICAjIyMgICAjIyAgICAgICAgICAgICAgICAgICAgICAgICAgIyMjIyMjIyMjIyMjIyMgICAgICAgICAgICAgICMiLAogICAgICAgICAgICAgIiMgICAgICAgICAgICAjIyMgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjIyMjIyMjIyMjIyMjIyAgICAgICAgICAgICAgICAjIiwKICAgICAgICAgICAgICIgICAgICAgICAgICAgICMjICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMjIyMjIyMjIyMjIyMiLAogICAgICAgICAgICAgIiAgICAgICAgICAgICMjIyMjICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMjIyMjIyMjIyMjICAgICAgICMjIiwKICAgICAgICAgICAgICIgICAgICAgICAgIyMjIyMjIyMjICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjIyMjIyMjIyMjICAgICAgIyMiLAogICAgICAgICAgICAgIiAgICAgICAgIyMjIyMjIyMjIyMjICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIyMjIyMjIyMjICAgICAjIyIsCiAgICAgICAgICAgICAiICAgICAgIyMjIyMjIyMjIyMjIyMjICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIyMjIyMjIyIsCiAgICAgICAgICAgICAiICAgICAjIyMjIyMjIyMjIyMjIyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMjIyMjICAgICAgICAgICAjIyMjIyMjIyMiLAogICAgICAgICAgICAgIiAgICAjIyMjIyMjIyMjIyMjIyMgIyMgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIyMjICAgICAgICAgICAjIyMjIyMjIyMjIyIsCiAgICAgICAgICAgICAiICAgICAjIyMjIyMjIyMjIyMjIyMgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIyAgICAgICAgICAgIyMjIyMjIyMjIyMjIiwKICAgICAgICAgICAgICIgICAgICAjIyMjIyMjIyMjIyMgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjIyMgICAjIyMjIiwKICAgICAgICAgICAgICIgICAgICAgIyMjIyMjIyMjICAgICAgIyIsCiAgICAgICAgICAgICAiIyAgICAgICAgICMjIyMjIiwKICAgICAgICAgICAgICIiLAogICAgICAgICAgICAgIiAgICAgICAgICAjIyMjIyMjIyAgICAgICAgICAgICAgICAgICAgICAgICMjIyMjIyAgICAgICAgICAgICAgICMjIyMjIyMiLAogICAgICAgICAgICAgIiAgICAgICAgIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyAjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMgICMjIyMjIyMjIyMjIyMjIiwKICAgICAgICAgICAgICIjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMiKQoKCiMgUnVuIGl0CnJ1bkl0KG1hcGRhdGEsIE5DT0wsIE5ST1cpCgoKCgo=