Advertisement
nikeza

Trojan Invasion

Oct 10th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class temp_MyTemp {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int wave = Integer.parseInt(scanner.nextLine());
  8.  
  9.         ArrayDeque<Integer> defensePlatQueue = new ArrayDeque<>();
  10.  
  11.         int[] defense = Arrays.stream(scanner.nextLine().split("\\s+"))
  12.                 .mapToInt(Integer::parseInt)
  13.                 .toArray();
  14.  
  15.         for (int i = 0; i < defense.length; i++) {
  16.             defensePlatQueue.offer(defense[i]);
  17.         }
  18.         int addBonusDefense = 0;
  19.  
  20.         //  Map<Integer, ArrayDeque<Integer>> attackArmyMap = new LinkedHashMap<>();
  21.         ArrayDeque<Integer> attackArmyStack = null;
  22.         for (int i = 1; i <= wave; i++) {
  23.             String line = scanner.nextLine();
  24.             if (i % 3 == 0) {
  25.                 addBonusDefense = Integer.parseInt(scanner.nextLine());
  26.                 defensePlatQueue.offer(addBonusDefense);
  27.             }
  28.             int[] atackPower = Arrays.stream(line.split("\\s+"))
  29.                     .mapToInt(Integer::parseInt)
  30.                     .toArray();
  31.             attackArmyStack = new ArrayDeque<>();
  32.  
  33.             for (int j = 0; j < atackPower.length; j++) {
  34.                 attackArmyStack.push(atackPower[j]);
  35.             }
  36.  
  37.  
  38.             int sizeAttack = attackArmyStack.size();
  39.             for (int j = 0; j < sizeAttack; j++) {
  40.                 int hit = 0;
  41.                 int attack = attackArmyStack.peek();
  42.                 int def = defensePlatQueue.peek();
  43.                 hit = def - attack;
  44.                 if (hit < 0) {
  45.                     defensePlatQueue.poll();
  46.                     attackArmyStack.pop();
  47.                     attackArmyStack.addFirst(Math.abs(hit));
  48.                     if (defensePlatQueue.isEmpty()) {
  49.                         break;
  50.                     }
  51.                     j--;
  52.  
  53.                 } else {
  54.                     if (hit == 0) {
  55.                         defensePlatQueue.poll();
  56.                         if (defensePlatQueue.isEmpty()) {
  57.                             attackArmyStack.pop();
  58.                             break;
  59.                         }
  60.                     } else {
  61.                         defensePlatQueue.poll();
  62.                         defensePlatQueue.addFirst(hit);
  63.                     }
  64.                     attackArmyStack.pop();
  65.  
  66.                 }
  67.             }
  68.  
  69.             if (defensePlatQueue.isEmpty()) {
  70.                 break;
  71.             }
  72.             if (attackArmyStack.isEmpty() && i == wave) {
  73.                 break;
  74.             }
  75.         }
  76.  
  77.         if (defensePlatQueue.isEmpty()) {
  78.             System.out.println("The Trojans successfully destroyed the Spartan defense.");
  79.             System.out.print("Warriors left: ");
  80.             System.out.println(attackArmyStack.toString().replaceAll("[\\[\\]]", ""));
  81.         } else {
  82.             System.out.println("The Spartans successfully repulsed the Trojan attack.");
  83.             System.out.print("Plates left: ");
  84.             System.out.println(defensePlatQueue.toString().replaceAll("[\\[\\]]", ""));
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement