Guest User

HeartDelivery

a guest
Mar 17th, 2020
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class HeartDelivery {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int[] houses = Arrays.stream(scanner.nextLine().split("@")).mapToInt(Integer::parseInt).toArray();
  9.  
  10.         String command;
  11.         int currentIndex = 0;
  12.  
  13.         while (!"Love!".equalsIgnoreCase(command = scanner.nextLine())) {
  14.             String[] tokens = command.split("\\s+");
  15.             int jumpLength = Integer.parseInt(tokens[1]);
  16.             currentIndex += jumpLength;
  17.             if (currentIndex >= houses.length) {
  18.                 currentIndex = 0;
  19.             }
  20.             if (houses[currentIndex] != 0) {
  21.                 houses[currentIndex] -= 2;
  22.                 if (houses[currentIndex] == 0) {
  23.                     System.out.printf("Place %d has Valentine's day.%n", currentIndex);
  24.                 }
  25.             } else {
  26.                 System.out.printf("Place %d already had Valentine's day.%n", currentIndex);
  27.             }
  28.         }
  29.         System.out.printf("Cupid's last position was %d.%n", currentIndex);
  30.         boolean isSuccessful = true;
  31.         for (int house : houses) {
  32.             if (house != 0) {
  33.                 isSuccessful = false;
  34.                 break;
  35.             }
  36.         }
  37.         int count = 0;
  38.         for (int house : houses) {
  39.             if (house != 0) {
  40.                 count++;
  41.             }
  42.         }
  43.         if (isSuccessful) {
  44.             System.out.println("Mission was successful.");
  45.         } else {
  46.             System.out.printf("Cupid has failed %d places.", count);
  47.         }
  48.     }
  49. }
Add Comment
Please, Sign In to add comment