ALSAVOV

Untitled

Nov 16th, 2023
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let index = 0;
  4.     let vaucherValue = Number(input[index]);
  5.     index++;
  6.     let command = input[index];
  7.     let filmCounter = 0;
  8.     let purchaseCounter = 0;
  9.  
  10.     while (vaucherValue > 0) {
  11.  
  12.         if (command === "End") {
  13.             break;
  14.         }
  15.        
  16.         let purchase = command;
  17.         let filmPrice = 0;
  18.         let purchasePrice = 0;
  19.  
  20.         let firstChar = purchase[0];
  21.         let secondChar = purchase[1];
  22.  
  23.         if (purchase.length > 8) {
  24.             filmPrice =
  25.                 Number(firstChar.charCodeAt()) +
  26.                 Number(secondChar.charCodeAt());
  27.  
  28.             if (vaucherValue - filmPrice < 0) {
  29.                 break;
  30.             }
  31.  
  32.             vaucherValue -= filmPrice;
  33.             filmCounter++;
  34.         } else {
  35.             purchasePrice = Number(firstChar.charCodeAt());
  36.  
  37.             if (vaucherValue - purchasePrice < 0) {
  38.                 break;
  39.             }
  40.  
  41.             vaucherValue -= purchasePrice;
  42.             purchaseCounter++;
  43.         }
  44.  
  45.         index++;
  46.         command = input[index];
  47.     }
  48.     console.log(filmCounter);
  49.     console.log(purchaseCounter);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment