Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace ConsoleApp76
  9. {
  10.     class Legion
  11.     {
  12.         public Legion(string legionName, long activity)
  13.         {
  14.             this.LegionName = legionName;
  15.             this.Activity = activity;
  16.         }
  17.  
  18.         public string LegionName { get; set; }
  19.         public long Activity { get; set; }
  20.        
  21.     }
  22.  
  23.  
  24.     class Program
  25.     {
  26.        
  27.  
  28.         static void Main(string[] args)
  29.         {
  30.             var dict = new Dictionary<Legion, Dictionary<string, long>>();
  31.             string validation = @"^[^=\->:\s]*$";
  32.             int number = int.Parse(Console.ReadLine());
  33.  
  34.             for (int i = 0; i < number; i++)
  35.             {
  36.                 string input = Console.ReadLine();
  37.                 string[] split = input.Split(new char[] { ' ', '=', '-', '>', ':' }, StringSplitOptions.RemoveEmptyEntries);
  38.                 long lastActivity = long.Parse(split[0]);
  39.                 string legionName = split[1];
  40.                 string soldierType = split[2];
  41.                 long soldierCount = long.Parse(split[3]);
  42.                 Regex check = new Regex(validation);
  43.                 if (!check.IsMatch(legionName) || !check.IsMatch(soldierType))
  44.                 {
  45.                     continue;
  46.                 }              
  47.                 if (!dict.Any(x => x.Key.LegionName == legionName))
  48.                 {
  49.                     Legion legion = new Legion(legionName, lastActivity);
  50.                     dict.Add(legion, new Dictionary<string, long>());
  51.                     dict[legion].Add(soldierType, soldierCount);
  52.                 }
  53.                 else
  54.                 {
  55.                     foreach (var item in dict.Where(x => x.Key.LegionName == legionName))
  56.                     {
  57.                         if (item.Key.Activity < lastActivity)
  58.                         {
  59.                             item.Key.Activity = lastActivity;
  60.                         }
  61.                     }
  62.                     var index = dict.Keys.FirstOrDefault(x => x.LegionName == legionName);
  63.                     if (dict[index].ContainsKey(soldierType))
  64.                     {
  65.                         dict[index][soldierType] += soldierCount;
  66.                     }
  67.                     else
  68.                     {
  69.                         dict[index][soldierType] = soldierCount;
  70.                     }
  71.                    
  72.                    
  73.                 }                            
  74.             }
  75.             string[] format = Console.ReadLine().Split('\\');
  76.             if (format.Length == 2)
  77.             {
  78.                 long activity = long.Parse(format[0]);
  79.                 string soldierType = format[1];
  80.  
  81.                 foreach (var item in dict
  82.                     .Where(x => x.Value.ContainsKey(soldierType))
  83.                     .OrderByDescending(x => x.Value[soldierType]))
  84.                 {
  85.                     if (item.Key.Activity < activity)
  86.                     {
  87.                         Console.WriteLine($"{item.Key.LegionName} -> {item.Value[soldierType]}");
  88.                     }
  89.                 }
  90.             }
  91.             else
  92.             {
  93.                 string soldierType = format[0];
  94.                 foreach (var item in dict.OrderByDescending(x => x.Key.Activity))
  95.                 {
  96.                     foreach (var z in item.Value.Where(x => x.Key == soldierType))
  97.                     {
  98.                         Console.WriteLine($"{item.Key.Activity} : {item.Key.LegionName}");
  99.                     }
  100.                 }
  101.  
  102.             }
  103.  
  104.  
  105.  
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement