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)
  center <- floor(NCOL/2)
  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, NROW)




