krasizorbov

Need For Speed III

Jul 8th, 2020
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(input = []) {
  2.   let map = new Map();
  3.   let count = +input.shift();
  4.   for (let i = 0; i < count; i++) {
  5.     let line = input[i].split("|");
  6.     let carName = line[0];
  7.     let mileage = +line[1];
  8.     let fuel = +line[2];
  9.     let obj = { mileage: mileage, fuel: fuel };
  10.     map.set(carName, obj);
  11.   }
  12.   for (let i = 0; i < input.length; i++) {
  13.     let line = input[i].split(" : ");
  14.     let command = line[0];
  15.     if (command === "Drive") {
  16.       let carName = line[1];
  17.       let distance = +line[2];
  18.       let fuel = +line[3];
  19.       let car = map.get(carName);
  20.       if (fuel > car.fuel) {
  21.         console.log("Not enough fuel to make that ride");
  22.       } else if (fuel <= car.fuel) {
  23.         car.fuel -= fuel;
  24.         car.mileage += distance;
  25.         console.log(
  26.           `${carName} driven for ${distance} kilometers. ${fuel} liters of fuel consumed.`
  27.         );
  28.         if (car.mileage >= 100000) {
  29.           console.log(`Time to sell the ${carName}!`);
  30.           map.delete(carName);
  31.         }
  32.       }
  33.     } else if (command === "Refuel") {
  34.       let carName = line[1];
  35.       let fuel = +line[2];
  36.       let car = map.get(carName);
  37.       if (car.fuel + fuel > 75) {
  38.         let additionalFuel = 75 - car.fuel;
  39.         car.fuel = 75;
  40.         console.log(`${carName} refueled with ${additionalFuel} liters`);
  41.       } else {
  42.         car.fuel += fuel;
  43.         console.log(`${carName} refueled with ${fuel} liters`);
  44.       }
  45.     } else if (command === "Revert") {
  46.       let carName = line[1];
  47.       let kilometers = +line[2];
  48.       let car = map.get(carName);
  49.       if (car.mileage - kilometers >= 10000) {
  50.         car.mileage -= kilometers;
  51.         console.log(`${carName} mileage decreased by ${kilometers} kilometers`);
  52.       } else {
  53.         car.mileage = 10000;
  54.       }
  55.     } else if (command === "Stop") {
  56.       break;
  57.     }
  58.   }
  59.   map = new Map(
  60.     [...map.entries()].sort(
  61.       (a, b) => sortByMileage(a, b) || sortByCarName(a, b)
  62.     )
  63.   );
  64.   for (const entries of map) {
  65.     console.log(
  66.       `${entries[0]} -> Mileage: ${entries[1].mileage} kms, Fuel in the tank: ${entries[1].fuel} lt.`
  67.     );
  68.   }
  69.   function sortByMileage(a, b) {
  70.     return b[1].mileage - a[1].mileage;
  71.   }
  72.   function sortByCarName(a, b) {
  73.     return a[0].localeCompare(b[0]);
  74.   }
  75. }
  76. solution([
  77.   "4",
  78.   "Lamborghini Veneno|11111|74",
  79.   "Bugatti Veyron|12345|67",
  80.   "Koenigsegg CCXR|67890|12",
  81.   "Aston Martin Valkryie|99900|50",
  82.   "Drive : Koenigsegg CCXR : 382 : 82",
  83.   "Drive : Aston Martin Valkryie : 99 : 23",
  84.   "Drive : Aston Martin Valkryie : 2 : 1",
  85.   "Refuel : Lamborghini Veneno : 40",
  86.   "Revert : Bugatti Veyron : 2000",
  87.   "Stop",
  88. ]);
Advertisement
Add Comment
Please, Sign In to add comment