SHARE
TWEET

Untitled

a guest Oct 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const problem = {
  2.     aix: {
  3.         simiane: 11,
  4.         marignane: 24,
  5.         vitrolles: 41,
  6.         bouc_bel_air: 26,
  7.         les_milles: 13,
  8.     },
  9.     simiane: {
  10.         aix: 11,
  11.         marignane: 17,
  12.         vitrolles: 27,
  13.         bouc_bel_air: 42,
  14.         les_milles: 28,
  15.     },
  16.     marignane: {
  17.         aix: 24,
  18.         simiane: 17,
  19.         vitrolles: 32,
  20.         bouc_bel_air: 51,
  21.         les_milles: 18,
  22.     },
  23.     vitrolles: {
  24.         aix: 41,
  25.         simiane: 27,
  26.         marignane: 32,
  27.         bouc_bel_air: 35,
  28.         les_milles: 32,
  29.     },
  30.     bouc_bel_air: {
  31.         aix: 26,
  32.         simiane: 42,
  33.         marignane: 31,
  34.         vitrolles: 51,
  35.         les_milles: 17,
  36.     },
  37.     les_milles: {
  38.         aix: 13,
  39.         simiane: 28,
  40.         marignane: 28,
  41.         vitrolles: 18,
  42.         bouc_bel_air: 17,
  43.     },
  44. }
  45.  
  46. const processed = [];
  47.  
  48. const takeTheLowest = (city) => {
  49.     const citiesChildren = Object.keys(city);
  50.     let lowest;
  51.     citiesChildren.forEach(cityChild => {
  52.         if (lowest) {
  53.             lowest = city[cityChild] < lowest ? city[cityChild] : lowest
  54.         } else {
  55.             lowest = problem[cityChild]
  56.         }
  57.     })
  58.  
  59.     return lowest;
  60. }
  61.  
  62. let lowest;
  63.  
  64. const createRoad = (start, end) => {
  65.     const roads = [
  66.         problem[start]
  67.     ];
  68.     let total = 0;
  69.  
  70.     while (roads[roads.length - 1] !== problem[end]) {
  71.         const value = takeTheLowest(roads[roads.length - 1]);
  72.         roads.push(value);
  73.         total += roads[roads.length - 1][value];
  74.         lowest = value;
  75.     }
  76.  
  77.     const cities = []
  78.  
  79.     roads.forEach(road => {
  80.         switch (road) {
  81.             case problem.aix:
  82.                 cities.push('Aix');
  83.                 break;
  84.             case problem.bouc_bel_air:
  85.                 cities.push('Bouc Bel Air');
  86.                 break;
  87.             case problem.les_milles:
  88.                 cities.push('Aix les Milles');
  89.                 break;
  90.             case problem.marignane:
  91.                 cities.push('Marignane');
  92.                 break;
  93.             case problem.simiane:
  94.                 cities.push('Simiane');
  95.                 break;
  96.             case problem.vitrolles:
  97.                 cities.push('Vitrolles');
  98.                 break;
  99.        
  100.             default:
  101.                 break;
  102.         }
  103.     })
  104.  
  105.     return {road, total}
  106. }
  107.  
  108. console.log(createRoad('aix', 'bouc_bel_air'));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top