Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let energy = Number(input.shift());
- let command = input.shift();
- let counter = 0;
- let isLost = false;
- while (command !== "End of battle") {
- let currentDistance = Number(command);
- energy -= currentDistance;
- counter++;
- if (counter % 3 === 0) {
- energy += counter;
- }
- if (energy <= 0) {
- isLost = true;
- console.log(
- `Not enough energy! Game ends with ${counter} won battles and ${energy} energy`
- );
- break;
- }
- command = input.shift();
- }
- if (command === "End of battle") {
- console.log(`Won battles: ${counter}. Energy left: ${energy}`);
- }
- }
- solve(["100", "10", "10", "10", "1", "2", "3", "73", "10"]);
- solve(["200", "54", "14", "28", "13", "End of battle"]);
Advertisement
Add Comment
Please, Sign In to add comment