Advertisement
Guest User

HornetArmada

a guest
Jul 20th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Legion
  6. {
  7.     public int LastActivity { get; set; }
  8.     public string LegionName { get; set; }
  9.     public List<LegionTypes> Soldiers { get; set; }
  10. }
  11.  
  12. class LegionTypes
  13. {
  14.     public string SoldierType { get; set; }
  15.     public long SoldierCount { get; set; }
  16. }
  17.  
  18. public class HornetArmada
  19. {
  20.     static int activity;
  21.     static string name;
  22.     static string type;
  23.     static long count;
  24.  
  25.     static string input = string.Empty;
  26.     static List<Legion> hornetLegions = new List<Legion>();
  27.  
  28.     public static void Main()
  29.     {
  30.         var commingLines = Convert.ToInt32(Console.ReadLine());
  31.         ReadNextInputLines(ref commingLines);
  32.         ReadPrintingCommandFrom(Console.ReadLine().Split('\\'));
  33.     }
  34.  
  35.     static void ReadPrintingCommandFrom(string[] commands)
  36.     {
  37.         if (commands.Length == 1)
  38.         {
  39.             PrintLegionsWithGivenSoldierType(commands[0]);
  40.             return;
  41.         }
  42.         else
  43.         {
  44.             PrintSoldiersFromLowerLastActivity(commands);
  45.         }
  46.     }
  47.  
  48.     static void PrintSoldiersFromLowerLastActivity(string[] commands)
  49.     {
  50.         var actyvity = Convert.ToInt32(commands[0]);
  51.         var soldiers = commands[1];
  52.         var output = hornetLegions
  53.             .Where(x => x.LastActivity < activity).ToDictionary(x => x.LegionName, x => x.Soldiers
  54.             .Where(s => s.SoldierType == soldiers).ToDictionary(s => s.SoldierType, s => s.SoldierCount))
  55.             .OrderByDescending(x => x.Value.Values.First());
  56.         foreach (var name in output)
  57.         {
  58.             foreach (var item in name.Value)
  59.             {
  60.                 Console.WriteLine($"{name.Key} -> {item.Value}");
  61.             }
  62.         }
  63.     }
  64.  
  65.     static void PrintLegionsWithGivenSoldierType(string type)
  66.     {
  67.         foreach (var item in hornetLegions.OrderByDescending(x => x.LastActivity))
  68.         {
  69.             if (item.Soldiers.Any(x => x.SoldierType == type))
  70.             {
  71.                 Console.WriteLine($"{item.LastActivity} : {item.LegionName}");
  72.             }
  73.         }
  74.     }
  75.  
  76.     static void ReadNextInputLines(ref int commingLines)
  77.     {
  78.         if (commingLines >= 1)
  79.         {
  80.             commingLines--;
  81.             input = Console.ReadLine();
  82.             SplitCurrentInput();
  83.             ParseSplitedInputToTheLegion();
  84.             ReadNextInputLines(ref commingLines);
  85.         }
  86.         else return;
  87.     }
  88.  
  89.     static void SplitCurrentInput()
  90.     {
  91.         var splitedInput = input.Split(new[] { " = ", " -> ", ":" }
  92.         , StringSplitOptions.RemoveEmptyEntries);
  93.         activity = Convert.ToInt32(splitedInput[0]);
  94.         name = splitedInput[1];
  95.         type = splitedInput[2];
  96.         count = Convert.ToInt64(splitedInput[3]);
  97.     }
  98.  
  99.     static void ParseSplitedInputToTheLegion()
  100.     {
  101.         if (!hornetLegions.Any(x => x.LegionName == name))
  102.         {
  103.             AddNewLegion();
  104.         }
  105.         else
  106.         {
  107.             UpdateCurrentLegion();
  108.         }
  109.     }
  110.  
  111.     static void AddNewLegion()
  112.     {
  113.         var newLegion = new Legion();
  114.         newLegion.LegionName = name;
  115.         newLegion.LastActivity = activity;
  116.         newLegion.Soldiers = new List<LegionTypes>();
  117.         var newType = new LegionTypes();
  118.         newType.SoldierType = type;
  119.         newType.SoldierCount = count;
  120.         newLegion.Soldiers.Add(newType);
  121.         hornetLegions.Add(newLegion);
  122.     }
  123.  
  124.     static void UpdateCurrentLegion()
  125.     {
  126.         var currentLegion = hornetLegions.Where(x => x.LegionName == name).First();
  127.         var newType = new LegionTypes();
  128.  
  129.         if (currentLegion.Soldiers.Any(x => x.SoldierType == type))
  130.         {
  131.             var currentType = currentLegion.Soldiers.Where(x => x.SoldierType == type).First();
  132.             currentType.SoldierCount += count;
  133.         }
  134.         else
  135.         {
  136.             newType.SoldierType = type;
  137.             newType.SoldierCount = count;
  138.             currentLegion.Soldiers.Add(newType);
  139.         }
  140.  
  141.         if (currentLegion.LastActivity < activity)
  142.         {
  143.             currentLegion.LastActivity = activity;
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement