Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function (mapString: string): number {
- let lines = mapString.split("\n")
- let height = lines.length
- let width = lines[0].length
- let holes = []
- let letters = []
- for (let i = 0; i < height; i++) {
- for (let j = 0; j < width; j ++) {
- if (lines[i][j].match(/^[0-9]+$/))
- holes.push([i, j, lines[i][j]])
- if (lines[i][j].match(/^[a-zA-Z]+$/))
- letters.push([i, j, lines[i][j]])
- }
- }
- let timeInSec = 0
- for (let i = 0; i < letters.length; i++) {
- let xl = letters[i][0]
- let yl = letters[i][1]
- let minSteps = height + width + 1
- for (let j = 0; j < holes.length; j++) {
- let xh = holes[j][0]
- let yh = holes[j][1]
- vSteps = Math.abs(xl - xh)
- hSteps = Math.abs(yl - yh)
- let time = vSteps + hSteps + 1
- if (time < minSteps)
- minSteps = time
- }
- if (minSteps > timeInSec) {
- timeInSec = minSteps
- // console.log(minSteps + 1);
- }
- }
- return timeInSec;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement