Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let shopList = input.shift().split(' ');
- let numberOfCommands = Number(input.shift());
- let commands = input;
- for (let line of input) {
- let tokens = line.split(' ');
- let command = tokens[0];
- if (command === 'Include') {
- shopList.push(tokens[1]);
- } else if (command === 'Visit') {
- let numberOfShops = Number(tokens[2]);
- if (numberOfShops <= shopList.length) {
- if (tokens[1] === 'first') {
- shopList.splice(0, numberOfShops);
- } else if (tokens[1] === 'last') {
- shopList.splice(shopList.length - numberOfShops, numberOfShops);
- }
- }
- } else if (command === 'Prefer') {
- let firstIndex = Number(tokens[1]);
- let secondIndex = Number(tokens[2]);
- if ((firstIndex >= 0 && firstIndex < shopList.length) && (secondIndex >= 0 && secondIndex < shopList.length)) {
- let firstShop = shopList[firstIndex];
- let secondShop = shopList[secondIndex];
- shopList[firstIndex] = secondShop;
- shopList[secondIndex] = firstShop;
- }
- } else if (command === 'Place') {
- let index = Number(tokens[2]) + 1;
- let shop = tokens[1];
- if (index >= 0 && index <= shopList.length) {
- shopList.splice(index, 0, shop);
- }
- }
- }
- console.log(`Shops left:\n${shopList.join(' ')}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement