Advertisement
Guest User

JavaScript

a guest
Feb 19th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let field = input
  3.     .shift()
  4.     .split("@")
  5.     .map(Number);
  6.  
  7.   let startIndex = 0;
  8.  
  9.   for (const line of input) {
  10.     if (line === `Love!`) {
  11.       break;
  12.     } else {
  13.       let jump = Number(line.split(" ")[1]);
  14.       let moveRight = jump % field.length;
  15.       let currentEl = startIndex + moveRight;
  16.       let newIndex = currentEl % field.length;
  17.       startIndex = newIndex;
  18.  
  19.       let el = field.splice(newIndex, 1)[0];
  20.  
  21.       let a = 5;
  22.       if (el === 0) {
  23.         console.log(`Place ${newIndex} had valentines day`);
  24.       } else {
  25.         el -= 2;
  26.       }
  27.  
  28.       field.splice(newIndex, 0, el);
  29.     }
  30.   }
  31.   console.log(`Cupidon's last position was ${startIndex}`);
  32.  let lengthField = field.filter(el => el != 0);
  33.  if (lengthField.length === 0) {
  34.    console.log(`Mission was successful.`);
  35.  } else {
  36.    console.log(`Cupidon has failed ${lengthField.length} places.`);
  37.  }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement