nikolayneykov

Untitled

Mar 6th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let commandProcessor = {
  3.         houses: input[1].split(' ').map(Number),
  4.         position: 0,
  5.         forward(numberOfSteps) {
  6.             if (this.position + numberOfSteps < this.houses.length) {
  7.                 this.position += numberOfSteps;
  8.                 this.houses.splice(this.position, 1);
  9.             }
  10.         },
  11.         back(numberOfSteps) {
  12.             if (this.position - numberOfSteps >= 0) {
  13.                 this.position -= numberOfSteps;
  14.                 this.houses.splice(this.position, 1);
  15.             }
  16.         },
  17.         gift(index, houseNumber) {
  18.             if (index >= 0 && index < this.houses.length) {
  19.                 this.houses.splice(index, 0, houseNumber);
  20.                 this.position = index;
  21.             }
  22.         },
  23.         swap(firstHouse, secondHouse) {
  24.             let firstIndex = this.houses.indexOf(firstHouse);
  25.             let secondIndex = this.houses.indexOf(secondHouse);
  26.             if (firstIndex !== -1 && secondIndex !== -1) {
  27.                 let temp = this.houses[firstIndex];
  28.                 this.houses[firstIndex] = this.houses[secondIndex];
  29.                 this.houses[secondIndex] = temp;
  30.             }
  31.         },
  32.         print() {
  33.             console.log(`Position: ${this.position}\n${this.houses.join(', ')}`);
  34.         }
  35.     }
  36.  
  37.     let commands = input.slice(2);
  38.     for (let line of commands) {
  39.         let tokens = line.split(' ');
  40.         let command = tokens[0].toLowerCase();
  41.         commandProcessor[command](+tokens[1], +tokens[2]);
  42.     }
  43.  
  44.     commandProcessor.print();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment