Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input = []) {
- let items = input.shift().split(', ');
- let commands = input.shift();
- while (commands !== 'Craft!') {
- let tokens = commands.split(' - ');
- let action = tokens[0];
- let item = tokens[1];
- let index = items.indexOf(item);
- switch (action) {
- case 'Collect':
- if (index < 0) {
- items.push(item)
- }
- break;
- case 'Drop':
- if (index >= 0) {
- items.splice(index, 1)
- }
- break;
- case 'Combine Items':
- let splitted = item.split(':');
- let old = splitted[0];
- let newItem = splitted[1];
- let newIndex = items.indexOf(old);
- if (newIndex >= 0) {
- items.splice(items.indexOf(newIndex[old]), 0, newItem);
- }
- case 'Renew':
- if (index >= 0) {
- items.push(item)
- items.shift()
- }
- }
- commands = input.shift()
- }
- console.log(items.join(", "))
- }
- solve([
- 'Iron, Sword',
- 'Drop - Bronze',
- 'Combine Items - Sword:Bow',
- 'Renew - Iron',
- 'Craft!'
- ])
Add Comment
Please, Sign In to add comment