Advertisement
TonyTroev

Roli The Coder

Feb 28th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _02_04_RoliTheCoder
  7. {
  8.     class RoliTheCoder
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Regex pattern = new Regex(@"(?<id>\d+)\s+#(?<event>.+?)($|(?=\ ))");
  13.             Regex participantsPattern = new Regex(@"@[a-zA-Z0-9'-]+");
  14.             Dictionary<int, Event> events = new Dictionary<int, Event>();
  15.             string input = string.Empty;
  16.  
  17.             while ((input = Console.ReadLine()) != "Time for Code")
  18.             {
  19.                 Match match = pattern.Match(input);
  20.                 if (match.Success)
  21.                 {
  22.                     int id = int.Parse(match.Groups["id"].Value);
  23.                     string name = match.Groups["event"].Value;
  24.  
  25.                     if (events.ContainsKey(id) && events[id].Name != name)
  26.                     {
  27.                         continue;
  28.                     }
  29.                     else if (!events.ContainsKey(id))
  30.                     {
  31.                         events.Add(id, new Event(name));
  32.                     }
  33.                     MatchCollection matches = participantsPattern.Matches(input);
  34.                     List<string> participants = new List<string>();
  35.                     foreach (Match m in matches)
  36.                     {
  37.                         participants.Add(m.Value);
  38.                     }
  39.                     events[id].AddParticipants(participants);
  40.                 }
  41.             }
  42.  
  43.             foreach (var pair in events.OrderByDescending(x => x.Value.Participants.Count).ThenBy(x => x.Value.Name))
  44.             {
  45.                 Console.WriteLine($"{pair.Value.Name} - {pair.Value.Participants.Count}");
  46.  
  47.                 foreach (string participant in pair.Value.Participants.OrderBy(x => x))
  48.                 {
  49.                     Console.WriteLine(participant);
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55.     class Event
  56.     {
  57.         public string Name { get; private set; }
  58.         public HashSet<string> Participants;
  59.  
  60.         public Event(string name)
  61.         {
  62.             this.Participants = new HashSet<string>();
  63.             this.Name = name;
  64.         }
  65.  
  66.         public void AddParticipants(List<string> participants)
  67.         {
  68.             foreach (string participant in participants)
  69.             {
  70.                 this.Participants.Add(participant);
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement