ErolKZ

Untitled

Nov 10th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4. let arr = [];
  5.  
  6. let countries = {};
  7.  
  8. let arr2 = [];
  9.  
  10.  
  11. for (let el of input) {
  12.  
  13. let cur = el.split(' > ');
  14.  
  15. let country = cur[0];
  16.  
  17. let town = cur[1];
  18.  
  19. let cost = cur[2];
  20.  
  21. if (countries.hasOwnProperty(country) && countries[country].hasOwnProperty(town)) {
  22.  
  23. countries[country][town].push(cost);
  24.  
  25. } else {
  26.  
  27. if (!countries.hasOwnProperty(country)) {
  28.  
  29. countries[country] = {};
  30.  
  31. }
  32.  
  33. if (!countries[country].hasOwnProperty(town)) {
  34.  
  35. countries[country][town] = [];
  36.  
  37. }
  38.  
  39.  
  40. countries[country][town].push(cost);
  41.  
  42. }
  43.  
  44.  
  45. }
  46.  
  47.  
  48. // console.log(countries);
  49.  
  50.  
  51. for (let key in countries) {
  52.  
  53. let smallest = 0;
  54.  
  55. label:
  56.  
  57. for (let nes in countries[key]) {
  58.  
  59.  
  60. smallest = countries[key][nes].sort((a, b) => a - b)[0];
  61.  
  62. for (let el of arr) {
  63.  
  64.  
  65. if (el.includes(key)) {
  66.  
  67. arr[arr[0].indexOf(key)][1].push([nes, smallest]);
  68.  
  69. arr2 = [];
  70.  
  71. continue label;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. arr2.push([nes, smallest]);
  78.  
  79. arr.push([key, arr2]);
  80.  
  81. arr2 = [];
  82.  
  83.  
  84. }
  85.  
  86.  
  87. }
  88.  
  89. for (let el of arr) {
  90.  
  91. el[1].sort((a, b) => a[1] - b[1]);
  92.  
  93. el[1] = el[1].flat();
  94.  
  95. }
  96.  
  97.  
  98. arr.sort((a, b) => a[0].localeCompare(b[0]));
  99.  
  100.  
  101. for (let el of arr) {
  102.  
  103. el = el.flat();
  104.  
  105. let output = '';
  106.  
  107. for (let el2 of el) {
  108.  
  109. if (el[el.indexOf(el2) + 1] !== undefined) {
  110.  
  111.  
  112. if (Number(el2)) {
  113.  
  114. output += `${el2} `;
  115.  
  116. } else {
  117.  
  118. output += `${el2} -> `;
  119.  
  120. }
  121.  
  122.  
  123. } else {
  124.  
  125. output += `${el2}`;
  126.  
  127. }
  128.  
  129. }
  130.  
  131.  
  132. console.log(output);
  133.  
  134.  
  135. }
  136.  
  137.  
  138. // console.log(arr);
  139.  
  140.  
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment