Advertisement
CR7CR7

hearthOfCupidon

Jun 15th, 2023
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Cupid {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         // Read the input string and split it by "@"
  9.         String[] input = scanner.nextLine().split("@");
  10.         // Convert the string array to an int array
  11.         int[] neighborhood = Arrays.stream(input).mapToInt(Integer::parseInt).toArray();
  12.         // Initialize the index of Cupid's position
  13.         int index = 0;
  14.         // Read the jump commands until "Love!"
  15.         String command = scanner.nextLine();
  16.         while (!command.equals("Love!")) {
  17.             // Extract the jump length from the command
  18.             int length = Integer.parseInt(command.split(" ")[1]);
  19.             // Update the index by adding the length
  20.             index += length;
  21.             // If the index is outside the neighborhood, start from the first house
  22.             if (index >= neighborhood.length) {
  23.                 index = 0;
  24.             }
  25.             // Decrease the needed hearts by 2
  26.             neighborhood[index] -= 2;
  27.             // Check if the house has Valentine's day or already had it
  28.             if (neighborhood[index] == 0) {
  29.                 System.out.println("Place " + index + " has Valentine's day.");
  30.             } else if (neighborhood[index] < 0) {
  31.                 System.out.println("Place " + index + " already had Valentine's day.");
  32.             }
  33.             // Read the next command
  34.             command = scanner.nextLine();
  35.         }
  36.         // Print Cupid's final position
  37.         System.out.println("Cupid's last position was " + index + ".");
  38.         // Count how many houses are successful and how many are not
  39.         int successful = 0;
  40.         int failed = 0;
  41.         for (int house : neighborhood) {
  42.             if (house <= 0) {
  43.                 successful++;
  44.             } else {
  45.                 failed++;
  46.             }
  47.         }
  48.         // Print the result
  49.         if (failed == 0) {
  50.             System.out.println("Mission was successful.");
  51.         } else {
  52.             System.out.println("Cupid has failed " + failed + " places.");
  53.         }
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement