borovaneca

HeartDelivery

Feb 16th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package Fundamentals.Exams;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class HeartDelivery {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         String neighborhood = scanner.nextLine();
  13.         List<Integer> neighborhoodList = Arrays.stream(neighborhood.split("@")).map(Integer::parseInt).collect(Collectors.toList());
  14.  
  15.         int index = 0;
  16.         int lastIndex = 0;
  17.  
  18.         String command = scanner.nextLine();
  19.         while (!command.equals("Love!")) {
  20.             String[] commandArray = command.split("\\s+");
  21.             int jumpLength = Integer.parseInt(commandArray[1]);
  22.             index += jumpLength;
  23.             if (index >= neighborhoodList.size()) {
  24.                 index = 0;
  25.             }
  26.  
  27.             if (neighborhoodList.get(index) == 0) {
  28.                 System.out.printf("Place %d already had Valentine's day.%n", index);
  29.             } else {
  30.                 int currentNeighborhood = neighborhoodList.get(index);
  31.                 neighborhoodList.set(index, currentNeighborhood - 2);
  32.                 if (neighborhoodList.get(index) == 0) {
  33.                     System.out.printf("Place %d has Valentine's day.%n", index);
  34.                 }
  35.             }
  36.  
  37.             lastIndex = index;
  38.             command = scanner.nextLine();
  39.         }
  40.  
  41.         System.out.printf("Cupid's last position was %d.%n", lastIndex);
  42.  
  43.         int houseCount = 0;
  44.         for (int i = 0; i <= neighborhoodList.size() - 1; i++) {
  45.             int current = neighborhoodList.get(i);
  46.             if (current > 0) {
  47.                 houseCount++;
  48.             }
  49.         }
  50.  
  51.         if (houseCount > 0) {
  52.             System.out.printf("Cupid has failed %d places.%n", houseCount);
  53.         } else if (houseCount == 0) {
  54.             System.out.printf("Mission was successful.%n");
  55.         }
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment