GerganaTsirkova

RolliTheCoder

Apr 7th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _3.RoliTheCoder
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var dict = new Dictionary<int, Dictionary<string, List<string>>>();
  13.             while (true)
  14.             {
  15.                 string command = Console.ReadLine();
  16.                 if (command == "Time for Code")
  17.                 {
  18.                     break;
  19.                 }
  20.                 if (IsValid(command) == false)
  21.                 {
  22.                     continue;
  23.                 }
  24.                 string[] tokens = command.Split().ToArray();
  25.                 int id = int.Parse(tokens[0]);
  26.                 string eventName = tokens[1].TrimStart('#');
  27.                 if (tokens.Length <= 2)
  28.                 {
  29.                     if (!dict.ContainsKey(id))
  30.                     {
  31.                         List<string> insideList = new List<string>();
  32.                         var insideDict = new Dictionary<string, List<string>>();
  33.                         insideDict.Add(eventName, insideList);
  34.                         dict.Add(id, insideDict);
  35.                         continue;
  36.                     }
  37.  
  38.                 }
  39.                 List<string> inner = new List<string>();
  40.                 for (int i = 2; i < tokens.Length; i++)
  41.                 {
  42.                     inner.Add(tokens[i]);
  43.                 }
  44.                 var innerList = inner.Distinct().ToList();
  45.                 if (!dict.ContainsKey(id))
  46.                 {
  47.                     var insideDict = new Dictionary<string, List<string>>();
  48.                     insideDict.Add(eventName, innerList);
  49.                     dict.Add(id, insideDict);
  50.                 }
  51.                 else
  52.                 {
  53.                     if (dict[id].ContainsKey(eventName))
  54.                     {
  55.                         for (int i = 0; i < innerList.Count; i++)
  56.                         {
  57.                             if (!dict[id][eventName].Contains(innerList[i]))
  58.                             {
  59.                                 dict[id][eventName].Add(innerList[i]);
  60.                             }
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.             foreach (var item in dict.OrderByDescending(x => dict[x.Key].Values.First().Count).ThenBy(y => dict[y.Key].Values.First()))
  66.             {
  67.                 foreach (var party in item.Value.OrderBy(x => x))
  68.                 {
  69.                     Console.WriteLine($"{party.Key} - {party.Value.Count}");
  70.                     foreach (var person in party.Value.OrderBy(x => x))
  71.                     {
  72.                         Console.WriteLine($"{person}");
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         static bool IsValid(string command)
  79.         {
  80.             string pattern = @"([0-9]+) (#[A-Za-z0-9]+)( (@[A-Za-z]+))*";
  81.             var match = Regex.Match(command, pattern);
  82.             if (match.Success)
  83.             {
  84.                 return true;
  85.             }
  86.             else
  87.             {
  88.                 return false;
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment