Advertisement
Guest User

Untitled

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