Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Trojan_Invasion
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. var waves = int.Parse(Console.ReadLine());
  12. var defencePlates = new Queue<int>(GetIntegerInput());
  13. bool winTrojans = false;
  14. for (int i = 0; i < waves; i++)
  15. {
  16. var currentWave = new Stack<int>(GetIntegerInput());
  17. if ((i+1) % 3 == 0) defencePlates.Enqueue(int.Parse(Console.ReadLine().Trim()));
  18. AttackWave(ref defencePlates, currentWave);
  19. if (defencePlates.Count == 0)
  20. {
  21. Console.WriteLine("The Trojans successfully destroyed the Spartan defense.");
  22. Console.WriteLine($"Warriors left: {string.Join(", ",currentWave)}");
  23. winTrojans = true;
  24. break;
  25. }
  26. }
  27. if (!winTrojans)
  28. {
  29. Console.WriteLine("The Spartans successfully repulsed the Trojan attack.");
  30. Console.WriteLine($"Plates left: {string.Join(", ",defencePlates)}");
  31. }
  32. }
  33.  
  34. private static void AttackWave(ref Queue<int> defencePlates, Stack<int> currentWave)
  35. {
  36. var currentPlate = -1;
  37. while (currentWave.Count>0)
  38. {
  39. if ((currentPlate < 0) && (defencePlates.Count > 0)) currentPlate = defencePlates.Dequeue();
  40. else if (currentPlate<0 && defencePlates.Count ==0) break;
  41. currentPlate -= currentWave.Pop();
  42. if (currentPlate < 0) currentWave.Push(currentPlate * -1);
  43. }
  44. if (currentPlate>0)
  45. {
  46. defencePlates = new Queue<int>(defencePlates.Reverse());
  47. defencePlates.Enqueue(currentPlate);
  48. defencePlates = new Queue<int>(defencePlates.Reverse());
  49. }
  50. }
  51.  
  52. private static IEnumerable<int> GetIntegerInput()
  53. {
  54. return Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement