Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (params) {
- let shopList = params[0].split(' ')
- params
- .slice(2)
- .map(x => x.split(' '))
- .forEach(([command, ...args]) => {
- if (command === 'Include') {
- let shop = args[0]
- shopList.push(shop)
- } else if (command === 'Visit') {
- let [removeCommand, count] = [args[0], +args[1]]
- if (count <= shopList.length && removeCommand === 'first') {
- shopList.splice(0, count)
- } else if (count <= shopList.length && removeCommand === 'last') {
- shopList.splice(-count, count)
- }
- } else if (command === 'Prefer') {
- let [index1, index2] = [+args[0], +args[1]]
- if (
- index1 >= 0 &&
- index1 < shopList.length &&
- index2 >= 0 &&
- index2 < shopList.length
- ) {
- let temp = shopList[index1]
- shopList[index1] = shopList[index2]
- shopList[index2] = temp
- }
- } else if (command === 'Place') {
- let [shop, shopIndex] = [args[0], +args[1]]
- if (shopIndex >= 0 && shopIndex < shopList.length) {
- shopList.splice(shopIndex + 1, 0, shop)
- }
- }
- })
- console.log(`Shops left:\n${shopList.join(' ')}`)
- }
- solve([
- 'Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore',
- '5',
- 'Include HM',
- 'Visit first 2',
- 'Visit last 1',
- 'Prefer 3 1',
- 'Place Library 2'
- ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement