Advertisement
t_sh0w

03. Need For Speed III

Apr 10th, 2020
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.   let carsQuantity = Number(input.shift());
  3.   let carsInfoArr = input.slice(0, carsQuantity);
  4.   let commandsInfo = input.slice(carsQuantity, input.length - 1);
  5.   let carsInfoObj = {};
  6.  
  7.   for (const currentCarInfo of carsInfoArr) {
  8.     let [car, currentMileage, currentFuel] = currentCarInfo.split("|");
  9.     currentMileage = Number(currentMileage);
  10.     currentFuel = Number(currentFuel);
  11.  
  12.     carsInfoObj[car] = {
  13.       mileage: currentMileage,
  14.       fuel: currentFuel,
  15.     };
  16.     // застраховка
  17.     if (carsInfoObj[car]["mileage"] >= 100000) {
  18.       console.log(`Time to sell the ${car}!`);
  19.       delete carsInfoObj[car];
  20.     }
  21.   }
  22.  
  23.   for (const currentLine of commandsInfo) {
  24.     let [command, car, tokenOne, tokenTwo] = currentLine.split(" : ");
  25.     tokenOne = Number(tokenOne);
  26.     tokenTwo = Number(tokenTwo);
  27.  
  28.     switch (command) {
  29.       case "Drive":
  30.         if (carsInfoObj[car]["fuel"] < tokenTwo) {
  31.           console.log("Not enough fuel to make that ride");
  32.         } else {
  33.           carsInfoObj[car]["fuel"] -= tokenTwo;
  34.           carsInfoObj[car]["mileage"] += tokenOne;
  35.           console.log(`${car} driven for ${tokenOne} kilometers. ${tokenTwo} liters of fuel consumed.`);
  36.  
  37.           if (carsInfoObj[car]["mileage"] >= 100000) {
  38.             console.log(`Time to sell the ${car}!`);
  39.             delete carsInfoObj[car];
  40.           }
  41.         }
  42.         break;
  43.  
  44.       case "Refuel":
  45.         if (carsInfoObj[car]["fuel"] + tokenOne > 75) {
  46.           console.log(`${car} refueled with ${75 - carsInfoObj[car]["fuel"]} liters`);
  47.           carsInfoObj[car]["fuel"] = 75;
  48.         } else {
  49.           console.log(`${car} refueled with ${tokenOne} liters`);
  50.           carsInfoObj[car]["fuel"] += tokenOne;
  51.         }
  52.         break;
  53.  
  54.       case "Revert":
  55.         if (carsInfoObj[car]["mileage"] - tokenOne < 10000) {
  56.           carsInfoObj[car]["mileage"] = 10000;
  57.         } else {
  58.           console.log(`${car} mileage decreased by ${tokenOne} kilometers`);
  59.           carsInfoObj[car]["mileage"] -= tokenOne;
  60.         }
  61.         break;
  62.     }
  63.   }
  64.   let entries = Object.entries(carsInfoObj)
  65.     .sort((a, b) => b[1]["mileage"] - a[1]["mileage"])
  66.     .sort((a, b) => {
  67.       if (a[1]["mileage"] === b[1]["mileage"]) {
  68.         return a[0].localeCompare(b[0]);
  69.       }
  70.     });
  71.   for (const currentEntry of entries) {
  72.     console.log(`${currentEntry[0]} -> Mileage: ${currentEntry[1]["mileage"]} kms, Fuel in the tank: ${currentEntry[1]["fuel"]} lt.`);
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement