Advertisement
CR7CR7

codeOfCeko

Jun 15th, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class HeartDelivery {
  7.     public static void main(String[] args) {
  8.         Scanner  scan = new Scanner(System.in);
  9.         List<Integer>  neededHeartsNeighbourhoods = Arrays.stream(scan.nextLine().split("@")).map(Integer::parseInt)
  10.                 .collect(Collectors.toList());
  11.         String input = scan.nextLine();
  12.         int lastIndex = 0;
  13.         int indexLength = 0;
  14.         while(!input.equals("Love!")){
  15.             int length = Integer.parseInt(input.split(" ")[1]);
  16.             indexLength+=length;
  17.             if(isInRange(indexLength,neededHeartsNeighbourhoods)){
  18.                 lastIndex=indexLength;
  19.                 int currentNum =neededHeartsNeighbourhoods.get(indexLength);
  20.                 if(currentNum==0){
  21.                     System.out.printf("Place %d already had Valentine's day.%n",indexLength);
  22.                     input=scan.nextLine();
  23.                     continue;
  24.                 }
  25.                 currentNum-=2;
  26.                 neededHeartsNeighbourhoods.set(indexLength,currentNum);
  27.                 if(neededHeartsNeighbourhoods.get(indexLength) == 0){
  28.                     System.out.printf("Place %d has Valentine's day.%n",indexLength);
  29.                 }
  30.             }else{
  31.                 // change this line to set lastIndex to 0
  32.                 lastIndex=0;
  33.                 length=0;
  34.                 indexLength=0;
  35.                 int currentNum =neededHeartsNeighbourhoods.get(length);
  36.                 if(currentNum==0){
  37.                     System.out.printf("Place %d already had Valentine's day.%n",indexLength);
  38.                     input=scan.nextLine();
  39.                     continue;
  40.                 }
  41.                 currentNum-=2;
  42.                 neededHeartsNeighbourhoods.set(indexLength,currentNum);
  43.                 if(neededHeartsNeighbourhoods.get(indexLength) == 0){
  44.                     System.out.printf("Place %d has Valentine's day.%n",indexLength);
  45.                 }
  46.             }
  47.             input=scan.nextLine();
  48.         }
  49.         System.out.printf("Cupid's last position was %d.%n",lastIndex);
  50.         if(checkHouses(neededHeartsNeighbourhoods)){
  51.             System.out.println("Mission successful.%n");
  52.         }else{
  53.             int times = failedTimes(neededHeartsNeighbourhoods);
  54.             System.out.printf("Cupid has failed %d places.%n",times);
  55.         }
  56.     }
  57.     public static boolean isInRange(int index, List<Integer> list){
  58.         return index>=0 && index<=list.size()-1;
  59.     }
  60.     public static boolean checkHouses(List<Integer> num){
  61.         for (int el:num) {
  62.             if(el>0){
  63.                 return false;
  64.             }
  65.         }
  66.         return true;
  67.     }
  68.     public static Integer failedTimes (List<Integer> list){
  69.         int times = 0;
  70.         for (int i = 0; i <list.size() ; i++) {
  71.             int currentNum = list.get(i);
  72.             if(currentNum>0){
  73.                 times++;
  74.             }
  75.         }
  76.         return times;
  77.     }
  78.  
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement