Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class HeartDelivery {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- List<Integer> neededHeartsNeighbourhoods = Arrays.stream(scan.nextLine().split("@")).map(Integer::parseInt)
- .collect(Collectors.toList());
- String input = scan.nextLine();
- int lastIndex = 0;
- int indexLength = 0;
- while(!input.equals("Love!")){
- int length = Integer.parseInt(input.split(" ")[1]);
- indexLength+=length;
- if(isInRange(indexLength,neededHeartsNeighbourhoods)){
- lastIndex=indexLength;
- int currentNum =neededHeartsNeighbourhoods.get(indexLength);
- if(currentNum==0){
- System.out.printf("Place %d already had Valentine's day.%n",indexLength);
- input=scan.nextLine();
- continue;
- }
- currentNum-=2;
- neededHeartsNeighbourhoods.set(indexLength,currentNum);
- if(neededHeartsNeighbourhoods.get(indexLength) == 0){
- System.out.printf("Place %d has Valentine's day.%n",indexLength);
- }
- }else{
- // change this line to set lastIndex to 0
- lastIndex=0;
- length=0;
- indexLength=0;
- int currentNum =neededHeartsNeighbourhoods.get(length);
- if(currentNum==0){
- System.out.printf("Place %d already had Valentine's day.%n",indexLength);
- input=scan.nextLine();
- continue;
- }
- currentNum-=2;
- neededHeartsNeighbourhoods.set(indexLength,currentNum);
- if(neededHeartsNeighbourhoods.get(indexLength) == 0){
- System.out.printf("Place %d has Valentine's day.%n",indexLength);
- }
- }
- input=scan.nextLine();
- }
- System.out.printf("Cupid's last position was %d.%n",lastIndex);
- if(checkHouses(neededHeartsNeighbourhoods)){
- System.out.println("Mission successful.%n");
- }else{
- int times = failedTimes(neededHeartsNeighbourhoods);
- System.out.printf("Cupid has failed %d places.%n",times);
- }
- }
- public static boolean isInRange(int index, List<Integer> list){
- return index>=0 && index<=list.size()-1;
- }
- public static boolean checkHouses(List<Integer> num){
- for (int el:num) {
- if(el>0){
- return false;
- }
- }
- return true;
- }
- public static Integer failedTimes (List<Integer> list){
- int times = 0;
- for (int i = 0; i <list.size() ; i++) {
- int currentNum = list.get(i);
- if(currentNum>0){
- times++;
- }
- }
- return times;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement