Skip to content

Instantly share code, notes, and snippets.

@MichaelDimitras
Last active June 13, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelDimitras/4c112196e843e2918a0805bbad2c5e38 to your computer and use it in GitHub Desktop.
Save MichaelDimitras/4c112196e843e2918a0805bbad2c5e38 to your computer and use it in GitHub Desktop.
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