Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. namespace RoliTheCoder
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text.RegularExpressions;
  7.     public class RoliTheCoder
  8.     {
  9.         static void Main()
  10.         {
  11.             var dict = new Dictionary<int, Event>();
  12.            
  13.  
  14.  
  15.             int id = 0;
  16.             string eventName = string.Empty;
  17.            
  18.             while (true)
  19.             {
  20.                 string input = Console.ReadLine().TrimStart();
  21.                 if (input == "Time for Code")
  22.                 {
  23.                     break;
  24.                 }
  25.  
  26.                 string[] tokens = input.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  27.                 string idPattern = @"(?<id>\d+)";
  28.                 Match firstMatch = Regex.Match(tokens[0], idPattern);
  29.                 if (firstMatch.Success)
  30.                 {
  31.                     id = int.Parse(firstMatch.Groups["id"].Value);
  32.                 }
  33.                 string eventPattern = @"#(?<event>[A-Za-z0-9_@]+)";
  34.                 Match secondMatch = Regex.Match(tokens[1], eventPattern);
  35.                 if (secondMatch.Success)
  36.                 {
  37.                     eventName = secondMatch.Groups["event"].Value;
  38.  
  39.                 }
  40.                 else
  41.                 {
  42.                     continue;
  43.                 }
  44.                 var list = new List<string>();
  45.                 string thirdPattern = @"@(?<participants>[A-Za-z0-9'-]+)";
  46.                 MatchCollection matches = Regex.Matches(input, thirdPattern);
  47.                 foreach (var item in matches)
  48.                 {
  49.                     list.Add(item.ToString());
  50.                    
  51.                 }
  52.                 list.Sort();
  53.                 if (!dict.ContainsKey(id))
  54.                 {
  55.                     dict.Add(id, new Event(eventName, id, list));
  56.                 }
  57.                 else
  58.                 {
  59.                     if (dict[id].Name != eventName)
  60.                     {
  61.                         continue;
  62.                     }
  63.                     else
  64.                     {
  65.                         if (dict[id].Name == eventName)
  66.                         {
  67.                             dict[id].Participants.AddRange(list);
  68.                             dict[id].Participants = dict[id].Participants.Distinct().ToList();
  69.                         }
  70.                     }
  71.                 }
  72.  
  73.             }
  74.             dict = dict.OrderByDescending(x => x.Value.Participants.Count()).ThenBy(x => x.Value.Name).ToDictionary(x => x.Key, x => x.Value);
  75.             foreach (var kvp in dict)
  76.             {
  77.                 //print format - {eventName} – {participantCount}
  78.                 Console.WriteLine($"{kvp.Value.Name} - {kvp.Value.Participants.Count()}");
  79.                 foreach (var participant in kvp.Value.Participants.OrderBy(x => x))
  80.                 {
  81.                     Console.WriteLine($"{participant}");
  82.                 }
  83.             }
  84.  
  85.         }
  86.     }
  87.     public class Event
  88.     {
  89.        public string Name { get; set; }
  90.  
  91.         public int Id { get; set; }
  92.  
  93.         public List<string> Participants { get; set; }
  94.  
  95.         public Event(string name,int id, List<string> participants)
  96.         {
  97.             this.Name = name;
  98.             this.Id = id;
  99.             this.Participants = participants;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement