Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class presentDelivery {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int[] houses = Arrays.stream(scanner.nextLine().split("@"))
  9.                 .mapToInt(Integer::parseInt)
  10.                 .toArray();
  11.  
  12.         String input = "";
  13.         int currentPosition = 0;
  14.         int lastPositionIndex = 0;
  15.  
  16.         while (!"Merry Xmas!".equals(input = scanner.nextLine())) {
  17.  
  18.             String[] command = input.split("\\s+");
  19.             int jumpLength = Integer.parseInt(command[1]);
  20.  
  21.             if (command[0].equals("Jump")) {
  22.  
  23.                 for (int i = 0; i < houses.length; i++) {
  24.                     if (jumpLength + currentPosition >= 0 && jumpLength + currentPosition < houses.length) {
  25.  
  26.                         currentPosition += jumpLength;
  27.                     } else {
  28.                         currentPosition = (currentPosition + jumpLength) % houses.length;
  29.                     }
  30.              
  31.                     lastPositionIndex = currentPosition;
  32.  
  33.                     if (houses[currentPosition] == 0) {
  34.                         System.out.printf("House %d will have a Merry Christmas.\n", currentPosition);
  35.                         break;
  36.                     } else {
  37.                         houses[currentPosition] -= 2;
  38.                         break;
  39.                     }
  40.  
  41.  
  42.                 }
  43.             }
  44.         }
  45.  
  46.         int failedHouses = 0;
  47.  
  48.         for (int i = 0; i < houses.length; i++) {
  49.             if (houses[i] != 0) {
  50.                 failedHouses++;
  51.             }
  52.         }
  53.  
  54.         System.out.printf("Santa's last position was %d.\n", lastPositionIndex);
  55.  
  56.         if (failedHouses == 0) {
  57.             System.out.println("Mission was successful.");
  58.         } else {
  59.             System.out.printf("Santa has failed %d houses.", failedHouses);
  60.         }
  61.  
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement