Advertisement
Guest User

Untitled

a guest
May 16th, 2020
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.   let houses = input.shift().split('@').map(Number);
  3.   let commands = input.shift();
  4.  
  5.   let currentIndex = 0;
  6.  
  7.   while (commands !== 'Love!') {
  8.     let tokens = commands.split(' ');
  9.     let jumpLength = +tokens[1];
  10.  
  11.     currentIndex += jumpLength;
  12.  
  13.     if (currentIndex >= houses.length) {
  14.       currentIndex = 0;
  15.     }
  16.  
  17.     if (houses[currentIndex] === 0) {
  18.       console.log(`Place ${currentIndex} already had Valentine's day.`);
  19.    } else {
  20.      houses[currentIndex] -= 2;
  21.      if (houses[currentIndex] === 0) {
  22.        console.log(`Place ${currentIndex} has Valentine's day.`);
  23.       }
  24.     }
  25.  
  26.     commands = input.shift();
  27.   }
  28.  
  29.   console.log(`Cupid's last position was ${currentIndex}.`);
  30.  
  31.  let isSuccess = true;
  32.  let count = 0;
  33.  
  34.  for (const house of houses) {
  35.    if (house !== 0) {
  36.      isSuccess = false;
  37.      count++;
  38.    }
  39.  }
  40.  
  41.  if (isSuccess) {
  42.    console.log(`Mission was successful.`);
  43.  } else {
  44.    console.log(`Cupid has failed ${count} places.`);
  45.  }
  46. }
  47.  
  48. solve(['10@10@10@2', 'Jump 1', 'Jump 2', 'Love!']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement