Skip to content

Instantly share code, notes, and snippets.

Created September 1, 2015 17:19
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 anonymous/62c519f612075d659814 to your computer and use it in GitHub Desktop.
Save anonymous/62c519f612075d659814 to your computer and use it in GitHub Desktop.
/*
JS-solution to:
https://tausiq.wordpress.com/2013/08/08/uab-2005-problem-6-shortest-path-in-alabama/
*/
var getRoutes = (function(){
var rawData = [
'Alabaster-Birmingham 24 miles',
'Alabaster-Montgomery 71 miles',
'Birmingham-Huntsville 103 miles',
'Birmingham-Tuscaloosa 59 miles',
'Demopolis-Mobile 141 miles',
'Demopolis-Montgomery 101 miles',
'Demopolis-Tuscaloosa 65 miles',
'Mobile-Montgomery 169 miles',
'Montgomery-Tuscaloosa 134 miles'
];
// transform raw data into towns and distances
var towns = [], dists = [];
rawData.forEach(function(x){
x = x.split(' ');
var y = x[0].split('-');
towns.indexOf(y[0]) < 0 && towns.push(y[0]);
towns.indexOf(y[1]) < 0 && towns.push(y[1]);
dists.push([y[1],y[0],x[1]/1]);
dists.push([y[0],y[1],x[1]/1]);
});
towns = towns.sort();
window.towns = towns; // export just for test gui
// find all routes
var routes = [], routemem = towns.slice(), routemem2, one;
// add zero distance routes (to-from same city)
towns.forEach(function(town){
routes.push([0,town,town]);
});
// add all other routes
do {
routemem2 = [];
towns.forEach(function(town){
routemem.forEach(function(route){
route = route.push ? route : [route];
dists.forEach(function(dist){
if(
dist[0] == route[route.length-1] &&
dist[1] == town &&
route.indexOf(town) < 0
){
one = route.concat(town);
if(one[0]/1 != one[0]){one.unshift(0);}
one[0] += dist[2];
routemem2.push(one);
}
});
});
});
routemem = routemem2;
routes = routes.concat(routemem);
} while (routemem2.length);
// order routes (shortest to longest)
routes.sort(function(x,y){
return x[0] < y[0] ? -1 : 1;
});
// get all routes between two cities (shortest first)
return function(from,to){
return routes.filter(function(x){
return x[1] == from && x[x.length-1] == to;
}).map(function(x){
x = x.slice();
var miles = x.shift();
return {route:x.join('-'),distance:miles + ' miles'};
});
}
})();
// Small GUI to test the solution
var selects = ' from <select id="from"><option value="">-</option>';
towns.forEach(function(town){
selects += '<option value="' + town + '">' + town + '</opton>';
});
selects += '</select>';
selects += selects.split('from').join('to');
html = '<h1>Route planner</h1><p>Travel ' + selects +
'</p><p id="result"></p>';
document.write(html);
function d(x){return document.getElementById(x);}
var from = d('from'), to = d('to'), result = d('result');
[from,to].forEach(function(x){
x.addEventListener('change',showResult);
});
function showResult(){
var r = getRoutes(from.value,to.value).map(function(x){
return '<span style="display:inline-block;width:600px">' +
x.route + '</span>' + x.distance;
});
r.length && r.unshift('<strong>Shortest route:</strong>');
r.length>2 && r.splice(2,0,"<br><strong>Other routes:</strong>");
result.innerHTML = r.join("<br>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment