Advertisement
Guest User

Trojan Invasion

a guest
Feb 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Deque;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. Scanner scan = new Scanner(System.in);
  14. int wavesCount = Integer.parseInt(scan.nextLine());
  15. Deque<Integer> spartansPlateQueue = new ArrayDeque<>();
  16. Arrays.stream(scan.nextLine().split("\\s+"))
  17. .map(Integer::parseInt)
  18. .forEach(spartansPlateQueue::offer);
  19.  
  20. Deque<Integer> trojanWarriorsStack = new ArrayDeque<>();
  21.  
  22. for (int i = 1; i <= wavesCount; i++) {
  23.  
  24. if (!spartansPlateQueue.isEmpty()) {
  25. Arrays.stream(scan.nextLine().split("\\s+"))
  26. .map(Integer::parseInt)
  27. .forEach(trojanWarriorsStack::push);
  28. }else {
  29. break;
  30. }
  31.  
  32. if (i % 3 == 0){
  33. spartansPlateQueue.addLast(Integer.parseInt(scan.nextLine()));
  34. }
  35.  
  36. while (!spartansPlateQueue.isEmpty() && !trojanWarriorsStack.isEmpty()){
  37.  
  38. int currentPlate = spartansPlateQueue.pop();
  39. int currentWarrior = trojanWarriorsStack.poll();
  40.  
  41. if (currentPlate != currentWarrior){
  42. if (currentWarrior > currentPlate){
  43. currentWarrior -= currentPlate;
  44. trojanWarriorsStack.push(currentWarrior);
  45. }else {
  46. currentPlate -= currentWarrior;
  47. spartansPlateQueue.addFirst(currentPlate);
  48. }
  49. }
  50.  
  51. }
  52. }
  53. if (spartansPlateQueue.isEmpty()){
  54. System.out.println("The Trojans successfully destroyed the Spartan defense.");
  55. System.out.println("Warriors left: " + trojanWarriorsStack.stream()
  56. .map(String::valueOf)
  57. .collect(Collectors.joining(", ")));
  58.  
  59. }else {
  60. System.out.println("The Spartans successfully repulsed the Trojan attack.");
  61. System.out.println("Plates left: " + spartansPlateQueue.stream()
  62. .map(String::valueOf)
  63. .collect(Collectors.joining(", ")));
  64. }
  65.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement