Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.04 KB | None | 0 0
  1. namespace Problem_4___Hornet_Armada
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text.RegularExpressions;
  7.  
  8.     class Program
  9.     {
  10.         class SingleLegionInfo
  11.         {
  12.             public int LastActivity { get; set; }
  13.             public string LegionName { get; set; }
  14.             public List<SingleSolderTypeAndCount> SoldierTypeList =
  15.                 new List<SingleSolderTypeAndCount>();
  16.         }
  17.  
  18.         class SingleSolderTypeAndCount
  19.         {
  20.             public string SoldierType { get; set; }
  21.             public /*int*/long UnitCount { get; set; }
  22.         }
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             List<SingleLegionInfo> legionInfoList = new List<SingleLegionInfo>();
  27.  
  28.             int countEntries = int.Parse(Console.ReadLine());
  29.  
  30.             for (int i = 0; i < countEntries; i++)
  31.             {
  32.                 string inputLine = Console.ReadLine();
  33.                 string[] tokens = inputLine.Split(
  34.                     new/*char*/string[] { " ", "=", "->", ":" },
  35.                     StringSplitOptions.RemoveEmptyEntries);
  36.  
  37.                 int lastActivityToken = int.Parse(tokens[0]);
  38.                 string legionNameToken = tokens[1];
  39.                 string soldierTypeToken = tokens[2];
  40.                 long unitCountToken = long.Parse(tokens[3]);
  41.  
  42.  
  43.                 // пъво проверряваш дали не са създадени вече:
  44.                 //SingleSolderTypeAndCount soldiersTypeAndUnitCount = new SingleSolderTypeAndCount();
  45.  
  46.                 //soldiersTypeAndUnitCount.SoldierType = soldierTypeToken;
  47.                 //soldiersTypeAndUnitCount.UnitCount = unitCountToken;
  48.  
  49.                 // non - existing
  50.  
  51.                 if (!legionInfoList.Any(x => x.LegionName == legionNameToken))
  52.                 {
  53.                     // ако няма такъв легион го създаваш и добавяш:
  54.                     SingleLegionInfo singleLegionData = new SingleLegionInfo();
  55.                     singleLegionData.LastActivity = lastActivityToken;
  56.                     singleLegionData.LegionName = legionNameToken;
  57.  
  58.                     legionInfoList.Add(singleLegionData);
  59.  
  60.                     // като създадеш легион тои си създава сам нов списък:
  61.                     // ако горе махнеш новия лист може да си го задаваш от тук:
  62.                     //singleLegionData.SoldierTypeList = new List<SingleSolderTypeAndCount>();
  63.  
  64.                     //това го правиш накрая на проверките и изобщо в цикъла
  65.                     //inputLine = Console.ReadLine();
  66.                     //continue;
  67.                 }
  68.  
  69.                 // existing
  70.  
  71.                
  72.                 SingleLegionInfo existingSingleLegionData =
  73.                     legionInfoList.Where(x => x.LegionName == legionNameToken).First();
  74.  
  75.                 // if the lastactivity need to change conditions
  76.  
  77.                 if (existingSingleLegionData.LastActivity < lastActivityToken)
  78.                 {
  79.                     existingSingleLegionData.LastActivity = lastActivityToken;
  80.                 }
  81.  
  82.  
  83.                 //List<SingleSolderTypeAndCount>existingSoldierTypeList=existingSingleLegionData.SoldierTypeLst;
  84.  
  85.                 // if there is a new type of soldier rank or else
  86.  
  87.                 if (!existingSingleLegionData.SoldierTypeList.Any(x => x.SoldierType == soldierTypeToken))
  88.                 {
  89.                     SingleSolderTypeAndCount newSoldiers = new SingleSolderTypeAndCount();
  90.                     newSoldiers.SoldierType = soldierTypeToken;
  91.                     newSoldiers.UnitCount = unitCountToken;
  92.  
  93.                     existingSingleLegionData.SoldierTypeList.Add(newSoldiers);
  94.                     //existingSoldierTypeList.Add(soldiersTypeAndUnitCount);
  95.  
  96.                     //inputLine = Console.ReadLine();
  97.                     //continue;
  98.                 }
  99.                 else
  100.                 {
  101.                     SingleSolderTypeAndCount existingSoldiers =
  102.                         existingSingleLegionData.SoldierTypeList
  103.                         .Where(x => x.SoldierType == soldierTypeToken).First();
  104.  
  105.                     existingSoldiers.UnitCount += unitCountToken;
  106.  
  107.                     // if a type of soldier already exist and we need to add unitCount to that type
  108.  
  109.                     //if (existingSoldierTypeList.Any(x => x.SoldierType == soldierTypeToken))
  110.                     //{
  111.                     //    SingleSolderTypeAndCount addUnits =existingSingleLegionData.SoldierTypeList.Firs(x= x.SoldierType ==soldierTypeToken);
  112.                     //    addUnits.UnitCount = addUnits.UnitCount + unitCountToken;
  113.                     //}
  114.                 }
  115.  
  116.                 //inputLine = Console.ReadLine();
  117.                 //continue;
  118.             }
  119.  
  120.             string[] lastRequest = Console.ReadLine()
  121.                 .Split(new[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
  122.  
  123.             //List<SingleLegionInfo> resultList = new List<SingleLegionInfo>();
  124.  
  125.             //string patterOne = @"([0-9]+\\[A-Za-z])\w+";
  126.             //Regex patterOptionOne = new Regex(patterOne);
  127.  
  128.             //bool containsValidMatchForFirst = patterOptionOne.IsMatch(inputLine);
  129.  
  130.             if (lastRequest.Length == 2)
  131.             {
  132.                 int activity = int.Parse(lastRequest[0]);
  133.                 string soldierType = lastRequest[1];
  134.  
  135.                 var output = legionInfoList.Where(x => x.LastActivity < activity)
  136.                     .ToDictionary(
  137.                     x => x.LegionName,
  138.                     x => x.SoldierTypeList.Where(y => y.SoldierType == soldierType)
  139.                     .ToDictionary(y => y.SoldierType, y => y.UnitCount))
  140.                     .OrderByDescending(c => c.Value.Values.FirstOrDefault());
  141.  
  142.                 foreach (var legion in output)
  143.                 {
  144.                     foreach (var sCount in legion.Value)
  145.                     {
  146.                         Console.WriteLine($"{legion.Key} -> {sCount.Value}");
  147.                     }
  148.                 }
  149.                 //Console.WriteLine(inputLine + " : First Type Input");
  150.  
  151.                 //string[] getInputeDate = inputLine.Split(new char[] { ' ', '\\' }, StringSplitOptions.RemoveEmptyEntries);
  152.                 //int findLastActivity = int.Parse(getInputeDate[0]);
  153.                 //string findType = getInputeDate[1];
  154.  
  155.                 //foreach (SingleLegionInfo item in legionInfoList)
  156.                 //{
  157.                 //    if (item.LastActivity != findLastActivity && item.SoldierTypeList.Any(x => (x.SoldierType == findType)))
  158.                 //    {
  159.                 //        SingleLegionInfo itemToList = item;
  160.                 //        resultList.Add(itemToList);
  161.                 //    }
  162.                 //}
  163.  
  164.                 //// нека първо подредим по големина на армията
  165.  
  166.                 //// място за код
  167.                 ////
  168.  
  169.                 //foreach (SingleLegionInfo item in resultList) //
  170.                 //{
  171.                 //    SingleSolderTypeAndCount element = item.SoldierTypeList.First(x => x.SoldierType == findType);
  172.                 //    int result = element.UnitCount;
  173.                 //    Console.WriteLine($"{item.LegionName} -> {result}");
  174.                 //}
  175.  
  176.             }
  177.             if(lastRequest.Length == 1)
  178.             {
  179.                 string soldierType = lastRequest[0];
  180.  
  181.                 foreach (var item in legionInfoList.OrderByDescending(x => x.LastActivity))
  182.                 {
  183.                     if (item.SoldierTypeList.Any(x => x.SoldierType == soldierType))
  184.                     {
  185.                         Console.WriteLine($"{item.LastActivity} : {item.LegionName}");
  186.                     }
  187.                 }
  188.                 // това няма нужда за сега
  189.                 //Console.WriteLine(inputLine + " : Second Type Input");
  190.  
  191.             }
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement