-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReto #19.js
More file actions
49 lines (41 loc) · 1.19 KB
/
Reto #19.js
File metadata and controls
49 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Reto #19: 🎄 El viaje secreto de Papá Noel
*
* ¡El GPS del trineo se ha vuelto loco! 😱 Papá Noel tiene los tramos de su viaje, pero están todos desordenados.
Tu misión es reconstruir la ruta completa desde el origen hasta el destino final.
Ten en cuenta: El primer elemento del array es siempre el primer tramo del viaje. A partir de ahí, debes ir conectando los destinos con los siguientes orígenes.
*/
console.log(revealSantaRoute([
['MEX', 'CAN'],
['UK', 'GER'],
['CAN', 'UK']
]));
// → ['MEX', 'CAN', 'UK', 'GER']
console.log(revealSantaRoute([
['USA', 'BRA'],
['JPN', 'PHL'],
['BRA', 'UAE'],
['UAE', 'JPN'],
['CMX', 'HKN']
]))
// → ['USA', 'BRA', 'UAE', 'JPN', 'PHL']
console.log(revealSantaRoute([
['STA', 'HYD'],
['ESP', 'CHN']
]));
// → ['STA', 'HYD']
/**
* @param {string[][]} routes - Array of [origin, destination] pairs
* @returns {string[]} The reconstructed route
*/
function revealSantaRoute(routes) {
const dictionary = Object.fromEntries(routes) ;
let currentCountry = routes[0][0];
let ans = [];
while(currentCountry)
{
ans.push(currentCountry);
currentCountry = dictionary[currentCountry];
}
return ans;
}