Guest User

Untitled

a guest
May 17th, 2020
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.     let items = input.shift().split(', ');
  3.     let commands = input.shift();
  4.  
  5.     while (commands !== 'Craft!') {
  6.         let tokens = commands.split(' - ');
  7.         let action = tokens[0];
  8.         let item = tokens[1];
  9.         let index = items.indexOf(item);
  10.  
  11.         switch (action) {
  12.             case 'Collect':
  13.                 if (index < 0) {
  14.                     items.push(item)
  15.                 }
  16.                 break;
  17.  
  18.             case 'Drop':
  19.                 if (index >= 0) {
  20.                     items.splice(index, 1)
  21.                 }
  22.                 break;
  23.  
  24.             case 'Combine Items':
  25.                 let splitted = item.split(':');
  26.                 let old = splitted[0];
  27.                 let newItem = splitted[1];
  28.                 let newIndex = items.indexOf(old);
  29.                 if (newIndex >= 0) {
  30.                     items.splice(items.indexOf(newIndex[old]), 0, newItem);
  31.                 }  
  32.             case 'Renew':
  33.                 if (index >= 0) {
  34.                     items.push(item)
  35.                     items.shift()
  36.                 }
  37.         }
  38.         commands = input.shift()
  39.     }
  40.  
  41.     console.log(items.join(", "))
  42.  
  43. }
  44.  
  45. solve([
  46.     'Iron, Sword',
  47.     'Drop - Bronze',
  48.     'Combine Items - Sword:Bow',
  49.     'Renew - Iron',
  50.     'Craft!'
  51. ])
Add Comment
Please, Sign In to add comment