Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let initialEnergy = Number(input.shift());
- let currentDistance = Number(input.shift());
- let battlesCount = 0;
- let wonCount = 0;
- let finished = true;
- while (currentDistance !== 'End of battle') {
- currentDistance = Number(currentDistance);
- if (initialEnergy >= currentDistance) {
- initialEnergy -= currentDistance;
- wonCount++;
- } else {
- battlesCount++;
- console.log(`Not enough energy! Game ends with ${wonCount} won battles and ${initialEnergy} energy`);
- finished = false;
- break;
- }
- if (wonCount % 3 === 0) {
- initialEnergy += wonCount;
- }
- currentDistance = input.shift();
- }
- if (finished) {
- console.log(`Won battles: ${wonCount}. Energy left: ${initialEnergy}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment