Didart

Heart Delivery

Oct 23rd, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 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
  9.                 .stream(scanner.nextLine().split("@"))
  10.                 .mapToInt(Integer::parseInt)
  11.                 .toArray();
  12.  
  13.         String command;
  14.         int currentIndex = 0;
  15.  
  16.         while (!"Love!".equalsIgnoreCase(command = scanner.nextLine())) {
  17.             String[] elements = command.split("\\s+");
  18.             int jumpLength = Integer.parseInt(elements[1]);
  19.             currentIndex += jumpLength;
  20.  
  21.             if (currentIndex >= houses.length) {
  22.                 currentIndex = 0;
  23.             }
  24.             if (houses[currentIndex] != 0) {
  25.                 houses[currentIndex] -= 2;
  26.                 if (houses[currentIndex] == 0) {
  27.                     System.out.printf("Place %d has Valentine's day.%n", currentIndex);
  28.                 }
  29.             } else {
  30.                 System.out.printf("Place %d already had Valentine's day.%n", currentIndex);
  31.             }
  32.         }
  33.         System.out.printf("Cupid's last position was %d.%n", currentIndex);
  34.         boolean isSuccessful = true;
  35.         for (int house : houses) {
  36.             if (house != 0) {
  37.                 isSuccessful = false;
  38.                 break;
  39.             }
  40.         }
  41.         int count = 0;
  42.         for (int house : houses) {
  43.             if (house != 0) {
  44.                 count++;
  45.             }
  46.         }
  47.         if (isSuccessful) {
  48.             System.out.println("Mission was successful.");
  49.         } else {
  50.             System.out.printf("Cupid has failed %d places.", count);
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment