Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr) {
- let inventory = inputArr.shift().split(", ");
- for (let i = 0; i < inputArr.length; i++) {
- let tokens = inputArr[i].split(" - ");
- if (tokens[0] == "Collect") {
- if (inventory.includes(tokens[1])) {
- continue;
- } else {
- inventory.push(tokens[1]);
- }
- } else if (tokens[0] == "Drop") {
- if (inventory.includes(tokens[1])) {
- let idx = inventory.indexOf(tokens[1]);
- inventory.splice(idx, 1);
- } else {
- continue;
- }
- } else if (tokens[0] == "Renew") {
- if (inventory.includes(tokens[1])) {
- let idx = inventory.indexOf(tokens[1]);
- let item = inventory.splice(idx, 1);
- inventory.push(item);
- } else {
- continue;
- }
- } else if (tokens[0] == "Combine Items") {
- let items = tokens[1].split(":");
- if (inventory.includes(items[0])) {
- let idx = inventory.indexOf(items[0]);
- inventory.splice(idx+1, 0, items[1]);
- } else {
- continue;
- }
- }
- }
- console.log(inventory.join(", "));
- }
- solve(['Iron, Wood, Sword', 'Collect - Gold', 'Drop - Wood', 'Craft!']);
- solve(['Iron, Sword', 'Drop - Bronze', 'Combine Items - Sword:Bow', 'Renew - Iron', 'Craft!'])
Add Comment
Please, Sign In to add comment