Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let numCommands = +input[0];
- let housesArray = input[1].split(' ');
- let position = 0;
- for (let i = 2; i < input.length; i++) {
- let elements = input[i].split(' ');
- let command = elements[0];
- let number = +elements[1];
- if (command === 'Forward' || command === 'Back') {
- if (command == 'Back') { //ако имаме Back трябва да се движим назад по масива
- number = -number;
- }
- if (position + number >= 0 && position + number < housesArray.length) { //проверяваме дали ще бъдем в рамките на масива
- position += number;
- housesArray.splice(position, 1);
- }
- }
- if (command === 'Gift') {
- let index = elements[1];
- if (index >= 0 && index < housesArray.length) { //проверяваме дали индекса е в рамките на масива
- let houseNumber = elements[2];
- housesArray.splice(index, 0, houseNumber);
- position=index; //сменяме позицията ни с новия индекс
- }
- }
- if (command === 'Swap') {
- let firstIndex = elements[1];
- let secondIndex = elements[2];
- if (housesArray.includes(firstIndex) && housesArray.includes(secondIndex)) {
- let index1 = housesArray.indexOf(firstIndex);
- let index2 = housesArray.indexOf(secondIndex);
- //разменяме отдолу стойностите
- let temp = housesArray[index1];
- housesArray[index1] = housesArray[index2];
- housesArray[index2] = temp;
- }
- }
- }
- console.log("Position: " + position);
- console.log(housesArray.join(", "));
- }
Advertisement
Add Comment
Please, Sign In to add comment