Last active
June 13, 2018 18:47
-
-
Save MichaelDimitras/4c112196e843e2918a0805bbad2c5e38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tunnelPath(startPoint, endPoint, tunnels) { | |
const manhattan = function(p1, p2) { | |
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]); | |
} | |
//init shortest to above ground between points | |
let shortestPath = manhattan(startPoint, endPoint); | |
const entrances = tunnels.map(item => [item[0], item[1]]); | |
//find above ground distance between all entrances | |
const distanceToEntrances = { | |
'start': entrances.map(p => manhattan(startPoint, p)), | |
'end': entrances.map(p => manhattan(endPoint, p)), | |
} | |
for(let i = 0; i < entrances.length; i++) { | |
//for all entrances track distance from start to entrance | |
//for each other entrance add junctions + distance to end | |
for(let j = 0; j < entrances.length; j++) { | |
let currentPath = distanceToEntrances.start[i] + tunnels[i][2] + tunnels[j][2] + distanceToEntrances.end[j]; | |
if (currentPath < shortestPath) { | |
shortestPath = currentPath; | |
} | |
} | |
} | |
return shortestPath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment