Advertisement
krasizorbov

Trojan Invasion

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