Advertisement
Guest User

Weaponsmith

a guest
Feb 28th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let particles = input.shift().split('|')
  3.     let line = input.shift()
  4.     while (line !== "Done") {
  5.         let arr = [];
  6.         if (line.includes("Left")) {
  7.             let index = Number(line.split(' ')[2])
  8.             if (index > 0  &&  index < particles.length) {
  9.                let temp = particles[index-1]
  10.                particles[index-1] = particles[index]
  11.                particles[index] = temp;
  12.             }
  13.         }  
  14.         else if (line.includes("Right")) {
  15.             let index = Number(line.split(' ')[2])
  16.             if (index >= 0 && index < particles.length -1 ) {
  17.                let temp = particles[index+1]
  18.                particles[index+1] = particles[index]
  19.                particles[index] = temp;
  20.              }
  21.         }
  22.         else if (line.includes("Even")){
  23.             for (let i = 0; i < particles.length; i+=2) {
  24.                 arr.push(particles[i])
  25.             }
  26.         }
  27.         else if (line.includes("Odd")){
  28.             for (let i = 1; i < particles.length; i+=2) {
  29.                 arr.push(particles[i])
  30.             }
  31.         }
  32.         line = input.shift()
  33.         if (arr.length > 0) {
  34.             console.log(arr.join(' '));
  35.         }
  36.     }
  37.     console.log(`You crafted ${particles.join('')}!`);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement