Advertisement
DyNaMiXx7

Untitled

Jun 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _01TrojanInvasion
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int wavesOfWarriors = int.Parse(Console.ReadLine());
  12.             List<int> plates = Console.ReadLine()
  13.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14.                 .Select(int.Parse)
  15.                 .ToList();
  16.  
  17.             Stack<int> stackOfWarriors = new Stack<int>();
  18.             bool isDestroyed = false;
  19.            
  20.             for (int wave = 1; wave <= wavesOfWarriors; wave++)
  21.             {
  22.                 List<int> powerOfWarriors = Console.ReadLine()
  23.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  24.                     .Select(int.Parse)
  25.                     .ToList();
  26.                 stackOfWarriors = new Stack<int>(powerOfWarriors);
  27.  
  28.                 if (wave % 3 == 0)
  29.                 {
  30.                     int extraWave = int.Parse(Console.ReadLine());
  31.                     plates.Add(extraWave);
  32.                 }
  33.  
  34.                 while (stackOfWarriors.Count > 0 && plates.Count > 0)
  35.                 {
  36.                     int currentWarrior = stackOfWarriors.Peek();
  37.  
  38.                     if (currentWarrior > plates[0])
  39.                     {
  40.                         currentWarrior -= plates[0];
  41.                         stackOfWarriors.Pop();
  42.                         stackOfWarriors.Push(currentWarrior);
  43.                         plates.RemoveAt(0);
  44.                     }
  45.                     else if (plates[0] > currentWarrior)
  46.                     {
  47.                         currentWarrior = stackOfWarriors.Pop();
  48.                         plates[0] -= currentWarrior;
  49.                     }
  50.                     else
  51.                     {
  52.                         stackOfWarriors.Pop();
  53.                         plates.RemoveAt(0);
  54.                     }
  55.  
  56.                     if (plates.Count == 0)
  57.                     {
  58.                         isDestroyed = true;
  59.                         break;
  60.                     }
  61.                 }
  62.  
  63.                 if (isDestroyed)
  64.                 {
  65.                     break;
  66.                 }
  67.             }
  68.  
  69.             if (plates.Count != 0)
  70.             {
  71.                 Console.WriteLine("The Spartans successfully repulsed the Trojan attack.");
  72.                 Console.WriteLine($"Plates left: {string.Join(", ", plates)}");
  73.             }
  74.             else
  75.             {
  76.                 Console.WriteLine("The Trojans successfully destroyed the Spartan defense.");
  77.                 Console.WriteLine($"Warriors left: {string.Join(", ", stackOfWarriors)}");
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement