Advertisement
simonradev

RoliTheCoder

Dec 8th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace RoliTheCoder
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<Event> allEvents = new List<Event>();
  15.  
  16.             string inputLine = Console.ReadLine();
  17.             while (!inputLine.Equals("Time for Code"))
  18.             {
  19.                 bool inputIsValid = CheckIfTheInputLineIsValid(inputLine.Trim());
  20.  
  21.                 if (!inputIsValid)
  22.                 {
  23.                     inputLine = Console.ReadLine();
  24.                     continue;
  25.                 }
  26.  
  27.                 string[] eventInfo = inputLine.Trim().
  28.                     Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  29.  
  30.                 int eventID = int.Parse(eventInfo[0]);
  31.                 string eventName = eventInfo[1].TrimStart('#');
  32.                 List<string> participants = new List<string>();
  33.                 participants = FillTheParticipantsIntoTheList(participants, eventInfo);
  34.                
  35.                 if (allEvents.Any(i => i.ID == eventID))
  36.                 {
  37.                     Event excistingEvent = allEvents.First(i => i.ID == eventID);
  38.  
  39.                     if (!excistingEvent.EventName.Equals(eventName))
  40.                     {
  41.                         inputLine = Console.ReadLine();
  42.                         continue;
  43.                     }
  44.                     excistingEvent.EventParticipants.AddRange(participants);
  45.                     excistingEvent.EventParticipants = excistingEvent.EventParticipants.Distinct().ToList();
  46.  
  47.                     inputLine = Console.ReadLine();
  48.                     continue;
  49.                 }
  50.  
  51.                 Event eventToAdd = new Event();
  52.                 eventToAdd.ID = eventID;
  53.                 eventToAdd.EventName = eventName;
  54.                 eventToAdd.EventParticipants.AddRange(participants);
  55.  
  56.                 allEvents.Add(eventToAdd);
  57.  
  58.                 inputLine = Console.ReadLine();
  59.             }
  60.  
  61.             PrintResult(allEvents);
  62.         }
  63.  
  64.         private static void PrintResult(List<Event> allEvents)
  65.         {
  66.             foreach (Event currEvent in allEvents.
  67.                                         OrderByDescending(x => x.EventParticipants.Count).
  68.                                         ThenBy(n => n.EventName))
  69.             {
  70.                 Console.WriteLine($"{currEvent.EventName} - {currEvent.EventParticipants.Count}");
  71.  
  72.                 foreach (string participant in currEvent.EventParticipants.OrderBy(x => x))
  73.                 {
  74.                     Console.WriteLine(participant);
  75.                 }
  76.             }
  77.         }
  78.  
  79.         private static List<string> FillTheParticipantsIntoTheList(List<string> participants, string[] eventInfo)
  80.         {
  81.             for (int currParticipant = 2; currParticipant < eventInfo.Length; currParticipant++)
  82.             {
  83.                 participants.Add(eventInfo[currParticipant]);
  84.             }
  85.  
  86.             return participants;
  87.         }
  88.  
  89.         private static bool CheckIfTheInputLineIsValid(string inputLine)
  90.         {
  91.             Regex checker = new Regex(@"^([0-9]+\s+#\w+(\s+@\w+)*)");
  92.  
  93.             string match = checker.Match(inputLine).Groups[1].ToString();
  94.  
  95.             return inputLine.Length == match.Length;
  96.         }
  97.     }
  98.  
  99.     class Event
  100.     {
  101.         public Event()
  102.         {
  103.             EventParticipants = new List<string>();
  104.         }
  105.  
  106.         public int ID { get; set; }
  107.  
  108.         public string EventName { get; set; }
  109.  
  110.         public List<string> EventParticipants { get; set; }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement