Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let commandProcessor = {
- houses: input[1].split(' ').map(Number),
- position: 0,
- forward(numberOfSteps) {
- if (this.position + numberOfSteps < this.houses.length) {
- this.position += numberOfSteps;
- this.houses.splice(this.position, 1);
- }
- },
- back(numberOfSteps) {
- if (this.position - numberOfSteps >= 0) {
- this.position -= numberOfSteps;
- this.houses.splice(this.position, 1);
- }
- },
- gift(index, houseNumber) {
- if (index >= 0 && index < this.houses.length) {
- this.houses.splice(index, 0, houseNumber);
- this.position = index;
- }
- },
- swap(firstHouse, secondHouse) {
- let firstIndex = this.houses.indexOf(firstHouse);
- let secondIndex = this.houses.indexOf(secondHouse);
- if (firstIndex !== -1 && secondIndex !== -1) {
- let temp = this.houses[firstIndex];
- this.houses[firstIndex] = this.houses[secondIndex];
- this.houses[secondIndex] = temp;
- }
- },
- print() {
- console.log(`Position: ${this.position}\n${this.houses.join(', ')}`);
- }
- }
- let commands = input.slice(2);
- for (let line of commands) {
- let tokens = line.split(' ');
- let command = tokens[0].toLowerCase();
- commandProcessor[command](+tokens[1], +tokens[2]);
- }
- commandProcessor.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment