Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function travelTime(input) {
- let countries = {};
- input.forEach(line => {
- let [country, town, price] = line.split(' > ');
- price = Number(price);
- if (!countries.hasOwnProperty(country)) {
- countries[country] = {};
- countries[country][town] = price;
- } else {
- let existingCountryObj = countries[country];
- if (!existingCountryObj.hasOwnProperty(town)) {
- countries[country][town] = price;
- } else {
- let oldPrice = countries[country][town];
- price = Math.min(oldPrice, price);
- countries[country][town] = price;
- }
- }
- });
- let sorted = Object.entries(countries).sort((firstPair, secondPair) => {
- let [firstCountry, firstData] = firstPair;
- let [secondCountry, secondData] = secondPair;
- return firstCountry.localeCompare(secondCountry);
- });
- for (let [name, towns] of sorted) {
- let sortedTowns = Object.entries(towns).sort((firstTown, secondTown) => {
- let firstPrice = firstTown[1];
- let secondPrice = secondTown[1];
- return firstPrice - secondPrice;
- });
- let output = `${name} -> `;
- for (let townName in towns) {
- let townPrice = towns[townName];
- output += `${townName} -> ${townPrice} `;
- }
- console.log(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement