SHARE
TWEET

TrojanInvasion

elvirynwa Jun 22nd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.reflect.Array;
  2. import java.util.ArrayDeque;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.util.StringJoiner;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.IntStream;
  8.  
  9. public class TrojanInvasion {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         int trojanWarriorsWaves = Integer.parseInt(scanner.nextLine());
  14.         int[] platesOfTheSpartans = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  15.  
  16.         ArrayDeque<Integer> spartans = new ArrayDeque<>();
  17.         for (int i = 0; i < platesOfTheSpartans.length; i++) {
  18.             spartans.offer(platesOfTheSpartans[i]);
  19.         }
  20.         ArrayDeque<Integer> trojans = new ArrayDeque<>();
  21.  
  22.         for (int i = 1; i <= trojanWarriorsWaves; i++) {
  23.             if (spartans.isEmpty()) {
  24.                 break;
  25.             }
  26.             int[] powerTrojan = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  27.             for (int warrior : powerTrojan) {
  28.                 trojans.push(warrior);
  29.             }
  30.             if (i % 3 == 0) {
  31.                 int sparta = Integer.parseInt(scanner.nextLine());
  32.                 spartans.offer(sparta);
  33.             }
  34.             while (!trojans.isEmpty() && !spartans.isEmpty()) {
  35.                 int attacker = trojans.pop();
  36.                 int defender = spartans.poll();
  37.                 if (attacker > defender) {
  38.                     attacker -= defender;
  39.                     trojans.push(attacker);
  40.                 } else if (defender > attacker) {
  41.                     defender -= attacker;
  42.                     spartans.offerFirst(defender);
  43.                 }
  44.             }
  45.         }
  46.         if (trojans.isEmpty()) {
  47.             System.out.println("The Spartans successfully repulsed the Trojan attack.");
  48.             System.out.printf("Plates left:%s",String.join(", ",spartans.toString()).replaceAll("[\\[\\]]",""));
  49.         } else {
  50.             System.out.println("The Trojans successfully destroyed the Spartan defense.");
  51.             System.out.printf("Warriors left:%s",String.join(", ",trojans.toString()).replaceAll("[\\[\\]]",""));
  52.  
  53.         }
  54.     }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top