Moving Target - 20/100
Здравейте,
Затруднявам се с тази изпитна задача - https://judge.softuni.bg/Contests/Practice/Index/2305#2 - засега само 20/100.
Публикувам кода тук, тъй като в момента не мога да вляза в Pastebin:
function solve(arr) {
let sequence = arr.shift().split(' ').map(Number);
let commands = arr.shift();
while (commands !== 'End') {
let splitted = commands.split(' ');
let command = splitted[0];
let index = Number(splitted[1]);
let value = Number(splitted[2]);
switch (command) {
case 'Shoot': shoot(index, value); break;
case 'Add': add(index, value); break;
case 'Strike': strike(index, value); break;
}
function shoot(index, power) {
if (sequence.includes(index)) {
sequence[index] -= power;
if (sequence[index] <= 0) {
sequence.splice(index, 1);
} else {
sequence.splice(index, 1, sequence[index]);
}
}
}
function add(index, value) {
if (sequence.includes(index)) {
sequence.splice(index, 0, value);
} else {
console.log(`Invalid placement!`);
}
}
function strike(index, radius) {
if (sequence.includes(index)) {
if ((index + radius) <= (sequence.length - 1) && (index - radius) >= 0) {
sequence.splice((index - radius), (radius * 2 + 1));
} else {
console.log(`Strike missed!`);
}
}
}
commands = arr.shift();
}
console.log(sequence.join('|'));
}
solve([
'52 74 23 44 96 110',
'Shoot 5 10',
'Shoot 1 80',
'Strike 2 1',
'Add 22 3',
'End'
]
)
Благодаря!