Advertisement
nikolayneykov

Untitled

Apr 30th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (params) {
  2.   let shopList = params[0].split(' ')
  3.  
  4.   params
  5.     .slice(2)
  6.     .map(x => x.split(' '))
  7.     .forEach(([command, ...args]) => {
  8.       if (command === 'Include') {
  9.         let shop = args[0]
  10.         shopList.push(shop)
  11.       } else if (command === 'Visit') {
  12.         let [removeCommand, count] = [args[0], +args[1]]
  13.         if (count <= shopList.length && removeCommand === 'first') {
  14.           shopList.splice(0, count)
  15.         } else if (count <= shopList.length && removeCommand === 'last') {
  16.           shopList.splice(-count, count)
  17.         }
  18.       } else if (command === 'Prefer') {
  19.         let [index1, index2] = [+args[0], +args[1]]
  20.  
  21.         if (
  22.           index1 >= 0 &&
  23.           index1 < shopList.length &&
  24.           index2 >= 0 &&
  25.           index2 < shopList.length
  26.         ) {
  27.           let temp = shopList[index1]
  28.  
  29.           shopList[index1] = shopList[index2]
  30.           shopList[index2] = temp
  31.         }
  32.       } else if (command === 'Place') {
  33.         let [shop, shopIndex] = [args[0], +args[1]]
  34.  
  35.         if (shopIndex >= 0 && shopIndex < shopList.length) {
  36.           shopList.splice(shopIndex + 1, 0, shop)
  37.         }
  38.       }
  39.     })
  40.  
  41.   console.log(`Shops left:\n${shopList.join(' ')}`)
  42. }
  43.  
  44. solve([
  45.   'Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore',
  46.   '5',
  47.   'Include HM',
  48.   'Visit first 2',
  49.   'Visit last 1',
  50.   'Prefer 3 1',
  51.   'Place Library 2'
  52. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement