Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let targets = arr.shift().split(' ').map(Number);
- let i = 0;
- let values = '';
- let shotTargets = [];
- let shotIndexes = [];
- let valueOfIndex = 0;
- let arrIncrease = [];
- // console.log(arr);
- while (arr[i] !== 'End') {
- let index = Number(arr[i]);
- // Keep in mind that you can't shoot a target, which is already shot. You also can't increase or reduce a target, which is considered shot.
- if (index < targets.length && !shotIndexes.includes(targets.indexOf(targets[index]))) {
- shotIndexes.push(index);
- arrIncrease = targets.slice();
- targets.forEach(num => num > targets[index] && !shotIndexes.includes(targets.indexOf(num)) ? targets[targets.indexOf(num)] -= targets[index] : false);
- valueOfIndex = arrIncrease[index];
- targets[index] = -1;
- arrIncrease.forEach(num => num <= valueOfIndex && !shotIndexes.includes(targets.indexOf(num)) && targets.indexOf(num) !== -1 ? targets[targets.indexOf(num)] += valueOfIndex : false);
- }
- i++;
- }
- shotTargets = targets.filter(num => num === -1);
- targets.forEach(num => values += `${num} `);
- console.log(`Shot targets: ${shotTargets.length} -> ${values}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment