Advertisement
Militsa

04. Roli The Coder

Dec 9th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04.Roli__The_Coder
  8. {
  9.  
  10.     public class Event
  11.     {
  12.         public string Name {get; set;}
  13.         public List<string> Participants {get; set;}
  14.  
  15.         public Event(string name,List<string> participants)
  16.         {
  17.             Name = name;
  18.             Participants = participants;
  19.         }
  20.     }
  21.  
  22.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             Regex regex = new Regex(@"^(?<id>\d+)\s*#(?<eventName>\w+)\s*(?<participants>(?:@[a-zA-Z\d'-]+\s*)*)$");
  27.             Dictionary<int, Event> dictEvents = new Dictionary<int, Event>();
  28.  
  29.             while (true)
  30.             {
  31.                 string line = Console.ReadLine();
  32.  
  33.                 if (line == "Time for Code")
  34.                 {
  35.                     break;
  36.                 }
  37.  
  38.                 Match eventMatch = regex.Match(line);
  39.                 if (eventMatch.Success == false)
  40.                 {
  41.                     continue;
  42.                 }
  43.  
  44.                 int id = int.Parse(eventMatch.Groups["id"].Value);
  45.                 string eventName = eventMatch.Groups["eventName"].Value;
  46.                 List<string> participants = eventMatch.Groups["participants"].Value.Split(new[] { ' ' },
  47.                     StringSplitOptions.RemoveEmptyEntries).ToList();
  48.  
  49.                 if (dictEvents.ContainsKey(id) == false)
  50.                 {
  51.                     dictEvents.Add(id, new Event(eventName, participants));
  52.                 }
  53.                 else if (dictEvents[id].Name == eventName)
  54.                 {
  55.                     dictEvents[id].Participants.AddRange(participants);
  56.                     dictEvents[id].Participants = dictEvents[id].Participants.Distinct().ToList();
  57.                 }
  58.             }
  59.             foreach (var e in dictEvents)
  60.             {
  61.                 e.Value.Participants = e.Value.Participants.Distinct().ToList();
  62.             }
  63.  
  64.             var ordered = dictEvents.Select(a => a.Value).OrderByDescending(e => e.Participants.Count).ThenBy(e => e.Name);
  65.  
  66.             foreach (var e in ordered)
  67.             {
  68.                 Console.WriteLine("{0} - {1}",e.Name,e.Participants.Count);
  69.                 foreach (var p in e.Participants.OrderBy(p => p))
  70.                 {
  71.                     Console.WriteLine(p);
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement