Advertisement
bac1lla

Untitled

Jul 5th, 2022
872
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.15 KB | None
  1. module.exports = function (mapString: string): number {
  2.        let lines = mapString.split("\n")
  3.     let height = lines.length
  4.     let width = lines[0].length
  5.  
  6.     let holes = []
  7.     let letters = []
  8.  
  9.     for (let i = 0; i < height; i++) {
  10.         for (let j = 0; j < width; j ++) {
  11.             if (lines[i][j].match(/^[0-9]+$/))
  12.                 holes.push([i, j, lines[i][j]])
  13.             if (lines[i][j].match(/^[a-zA-Z]+$/))
  14.                 letters.push([i, j, lines[i][j]])
  15.  
  16.         }
  17.     }
  18.  
  19.     let timeInSec = 0
  20.  
  21.     for (let i = 0; i < letters.length; i++) {
  22.         let xl = letters[i][0]
  23.         let yl = letters[i][1]
  24.  
  25.         let minSteps = height + width + 1
  26.  
  27.         for (let j = 0; j < holes.length; j++) {
  28.             let xh = holes[j][0]
  29.             let yh = holes[j][1]
  30.  
  31.             vSteps = Math.abs(xl - xh)
  32.             hSteps = Math.abs(yl - yh)
  33.             let time = vSteps + hSteps + 1
  34.             if (time < minSteps)
  35.                 minSteps = time
  36.         }
  37.         if (minSteps > timeInSec) {
  38.             timeInSec = minSteps
  39.             // console.log(minSteps + 1);
  40.         }
  41.  
  42.     }
  43.  
  44.  
  45.     return timeInSec;
  46. }
Advertisement
RAW Paste Data Copied
Advertisement