Advertisement
Guest User

Untitled

a guest
May 16th, 2020
506
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 command = tokens[0];
  10.         let jumpLength = +tokens[1];
  11.  
  12.         currentIndex += jumpLength;
  13.  
  14.         if (currentIndex >= houses.length) {
  15.             currentIndex = 0;
  16.         }
  17.  
  18.         if (houses[currentIndex] !== 0) {
  19.             houses[currentIndex] -= 2;
  20.  
  21.             if (houses[currentIndex] === 0) {
  22.                 console.log(`Place ${currentIndex} has Valentine's day.`);
  23.            } else {
  24.                console.log(`Place ${currentIndex} already had Valentine's day.`);
  25.             }
  26.         }
  27.  
  28.         commands = input.shift();
  29.     }
  30.  
  31.     console.log(`Cupid's last position was ${currentIndex}.`);
  32.  
  33.    let isSuccess = true;
  34.  
  35.    for (const house of houses) {
  36.        if (house !== 0) {
  37.            isSuccess = false;
  38.            break;
  39.        }
  40.    }
  41.  
  42.    let count = 0;
  43.  
  44.    for (const house of houses) {
  45.        if (house !== 0) {
  46.            count++;
  47.        }
  48.    }
  49.  
  50.    if (isSuccess) {
  51.        console.log(`Mission successful.`);
  52.    } else {
  53.        console.log(`Cupid has failed ${count} places.`);
  54.    }
  55. }
  56.  
  57. solve(['10@10@10@2', 'Jump 1', 'Jump 2', 'Love!'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement