Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let targets = input.shift().split(' ').map(Number);
- let length = input.length;
- for (let i = 0; i < length; i++){
- let currentCommand = input.shift().split(' ');
- let action = currentCommand.shift();
- let currIndex = currentCommand.shift();
- currIndex = Number(currIndex);
- if (action === 'Shoot'){
- let power = currentCommand.shift();
- power = Number(power);
- for (let i = 0; i < targets.length; i++){
- if (i === currIndex){
- targets[i] -= power;
- if (targets[i] <= 0){
- targets.splice(i,1);
- }
- }
- }
- } else if (action === 'Add'){
- let value = currentCommand.shift();
- value = Number(value);
- let isValid = false;
- for (let i = 0; i < targets.length; i++){
- if (i === currIndex){
- isValid = true;
- targets.splice(i,0,value);
- }
- }
- if (!isValid){
- console.log('Invalid placement!');
- }
- } else if (action === 'Strike'){
- let radius = currentCommand.shift();
- radius = Number(radius);
- let isStrike = false;
- for (let i = 0; i < targets.length; i++){
- if (i === currIndex){
- if ((i - radius !== -1 && i - radius !== undefined) && (radius+i !== -1 && radius+1 !== undefined)){
- targets.splice(i - radius,radius * 2 +1);
- isStrike = true;
- }
- }
- }
- if(!isStrike){
- console.log('Strike missed!');
- }
- }
- }
- console.log(targets.join('|'));
- }
Add Comment
Please, Sign In to add comment