Advertisement
Grossos

03-man-o-war.js

Oct 21st, 2023
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Man-O-War
  2. // The pirates encounter a huge Man-O-War at sea.
  3. // Create a program that tracks the battle and either chooses a winner or prints a stalemate. On the first line, you will receive the status of the pirate ship, which is a string representing integer sections separated by ">". On the second line, you will receive the same type of status, but for the warship:
  4. // "{section1}>{section2}>{section3}… {sectionn}"
  5. // On the third line, you will receive the maximum health capacity a section of the ship can reach.
  6. // The following lines represent commands until "Retire":
  7. // "Fire {index} {damage}" - the pirate ship attacks the warship with the given damage at that section. Check if the index is valid and if not, skip the command. If the section breaks (health <= 0) the warship sinks, print the following and stop the program: "You won! The enemy ship has sunken."
  8. // "Defend {startIndex} {endIndex} {damage}" - the warship attacks the pirate ship with the given damage at that range (indexes are inclusive). Check if both indexes are valid and if not, skip the command. If the section breaks (health <= 0) the pirate ship sinks, print the following and stop the program:
  9. // "You lost! The pirate ship has sunken."
  10. // "Repair {index} {health}" - the crew repairs a section of the pirate ship with the given health. Check if the index is valid and if not, skip the command. The health of the section cannot exceed the maximum health capacity.
  11. // "Status" - prints the count of all sections of the pirate ship that need repair soon, which are all sections that are lower than 20% of the maximum health capacity. Print the following:
  12. // "{count} sections need repair."
  13. // In the end, if a stalemate occurs, print the status of both ships, which is the sum of their individual sections, in the following format:
  14. // "Pirate ship status: {pirateShipSum}
  15. // Warship status: {warshipSum}"
  16. // Input
  17. // On the 1st line, you are going to receive the status of the pirate ship (integers separated by '>').
  18. // On the 2nd line, you are going to receive the status of the warship.
  19. // On the 3rd line, you will receive the maximum health a section of a ship can reach.
  20. // On the following lines, until "Retire", you will be receiving commands.
  21. // Output
  22. // Print the output in the format described above.
  23. // Constraints
  24. // The section numbers will be integers in the range [1….1000].
  25. // The indexes will be integers [-200….200].
  26. // The damage will be an integer in the range [1….1000].
  27. // The health will be an integer in the range [1….1000].
  28.  
  29.  
  30.  
  31. function solve(input) {
  32.  
  33.     let pirate = input.shift().split('>').map(Number);
  34.     let warship = input.shift().split('>').map(Number);
  35.     let maxHP = +input.shift();
  36.  
  37.     let count = 0;
  38.     for (let current of input) {
  39.         let tokens = current.split(' ');
  40.         let command = tokens.shift();
  41.  
  42.         if (command == 'Retire') {
  43.             break;
  44.         } else if (command == 'Fire') {
  45.             let index = +tokens[0];
  46.             let damage = +tokens[1];
  47.  
  48.             if (index < 0 || index >= warship.length) {
  49.                 continue;
  50.             }
  51.  
  52.             warship[index] -= damage;
  53.  
  54.             if (warship[index] <= 0) {
  55.                 console.log(`${count} sections need repair.`);
  56.                 console.log(`You won! The enemy ship has sunken.`);
  57.                 return;
  58.             }
  59.  
  60.             //the warship attacks the pirate ship with the given damage at that range (indexes are inclusive).
  61.         } else if (command == 'Defend') {
  62.             let start = +tokens[0];
  63.             let end = +tokens[1];
  64.             let damage = +tokens[2];
  65.  
  66.             if (start < 0 || start >= pirate.length || end < 0 || end >= pirate.length) {
  67.                 continue;
  68.             }
  69.  
  70.             for (let i = start; i <= end; i++) {
  71.                 pirate[i] -= damage;
  72.  
  73.                 if (pirate[i] <= 0) {
  74.                     console.log(`${count} sections need repair.`);
  75.                     console.log(`You lost! The pirate ship has sunken.`);
  76.                     return;
  77.                 }
  78.             }
  79.  
  80.         } else if (command == 'Repair') {
  81.             let index = +tokens[0];
  82.             let health = +tokens[1];
  83.  
  84.             if (index < 0 || index >= pirate.length) {
  85.                 continue;
  86.             }
  87.  
  88.             pirate[index] += health;
  89.             if (pirate[index] > maxHP) {
  90.                 pirate[index] = maxHP;
  91.             }
  92.  
  93.         } else if (command == 'Status') {
  94.             for (let i = 0; i < pirate.length; i++) {
  95.                 if (pirate[i] < maxHP * 0.2) {
  96.                     count++;
  97.                 }
  98.             }
  99.         }
  100.  
  101.     }
  102.  
  103.     let pirateStatus = 0;
  104.     let warshipStatus = 0;
  105.  
  106.     for (let section of pirate) {
  107.         pirateStatus += section;
  108.     }
  109.  
  110.     for (let section of warship) {
  111.         warshipStatus += section;
  112.     }
  113.     console.log(`${count} sections need repair.`);
  114.     console.log(`Pirate ship status: ${pirateStatus}`);
  115.     console.log(`Warship status: ${warshipStatus}`);
  116.  
  117. }
  118. // solve(["12>13>11>20>66", "12>22>33>44>55>32>18",
  119. //     "70",
  120. //     "Fire 2 11",
  121. //     "Fire 8 100",
  122. //     "Defend 3 6 11",
  123. //     "Defend 0 3 5",
  124. //     "Repair 1 33",
  125. //     "Status",
  126. //     "Retire"])
  127.  
  128. solve(["2>3>4>5>2",
  129.     "6>7>8>9>10>11",
  130.     "20",
  131.     "Status",
  132.     "Fire 2 3",
  133.     "Defend 0 4 11",
  134.     "Repair 3 18",
  135.     "Retire"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement