Advertisement
damesova

Trojan Invasion [Mimi][JA]

Jun 22nd, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class _01_TrojanInvasion {
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         int waves = Integer.parseInt(sc.nextLine());
  12.         //Queue:
  13.         ArrayDeque<Integer> defenders =
  14.                 Arrays.stream(sc.nextLine()
  15.                         .split("\\s+"))
  16.                         .map(Integer::parseInt)
  17.                         .collect(Collectors.toCollection(ArrayDeque::new));
  18.  
  19.         //Stack:
  20.         ArrayDeque<Integer> attackers = new ArrayDeque<>();
  21.         for (int i = 1; i <= waves && !defenders.isEmpty(); i++) {
  22.             Arrays.stream(sc.nextLine()
  23.                     .split("\\s+"))
  24.                     .map(Integer::parseInt)
  25.                     .forEach(attackers::push);
  26.  
  27.             if (i % 3 == 0){
  28.               int extraDef = Integer.parseInt(sc.nextLine());
  29.               defenders.offer(extraDef);
  30.             }
  31.  
  32.             while (!attackers.isEmpty() && !defenders.isEmpty()){
  33.  
  34.                 int attack = attackers.pop();
  35.                 int defense = defenders.poll();
  36.  
  37.                 if (attack > defense){
  38.                     attack -= defense;
  39.                     attackers.push(attack);
  40.                 }else if(defense > attack){
  41.                     defense -= attack;
  42.                     defenders.addFirst(defense);
  43.                 }
  44.             }
  45.         }
  46.  
  47.  
  48.         if (defenders.isEmpty()){
  49.             System.out.println("The Trojans successfully destroyed the Spartan defense.");
  50.             System.out.printf("Warriors left: %s%n",
  51.                     String.join(", ", attackers.toString().replaceAll("[\\[\\]]", "")));
  52.  
  53.         } else {
  54.             System.out.println("The Spartans successfully repulsed the Trojan attack.");
  55.             System.out.printf("Plates left: %s%n",
  56.                     String.join(", ", defenders.toString().replaceAll("[\\[\\]]", "")));
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement