Advertisement
Rayk

Untitled

Nov 3rd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _04_Roli_The_Coder
  7. {
  8. public class RoliTheCoder
  9. {
  10. public static void Main()
  11. {
  12. var eventAndIdRegex = new Regex(@"^(?<id>\d+)\s+(?<event>#\w+)");
  13. var participantsRegex = new Regex(@"@(?<participant>[A-Za-z\d\-\']*)");
  14.  
  15. List<Event> events = new List<Event>();
  16. while (true)
  17. {
  18. string line = Console.ReadLine();
  19. if (line == "Time for Code")
  20. break;
  21.  
  22. var eventAndIdMatch = eventAndIdRegex.Match(line);
  23. if (!eventAndIdMatch.Success)
  24. continue;
  25.  
  26. var eventId = int.Parse(eventAndIdMatch.Groups["id"].Value);
  27. var eventName = eventAndIdMatch.Groups["event"].Value;
  28.  
  29. var participants = participantsRegex.Matches(line).Cast<Match>().Select(p => p.Value.Trim()).ToList();
  30.  
  31. if (events.Any(e => e.Id == eventId))
  32. {
  33. var alreadyExistingEvent = events.First(e => e.Id == eventId);
  34.  
  35. if (alreadyExistingEvent.EventName == eventName)
  36. alreadyExistingEvent.Participants.AddRange(participants);
  37.  
  38. }
  39. else
  40. {
  41. var @event = new Event(eventId, eventName, new List<string>());
  42. @event.Participants.AddRange(participants);
  43. events.Add(@event);
  44. }
  45. }
  46.  
  47. foreach (var @event in events)
  48. {
  49. @event.Participants = @event.Participants.Distinct().ToList();
  50. }
  51.  
  52. //PRINT
  53. foreach (var @event in events.OrderByDescending(e => e.Participants.Count).ThenBy(e => e.EventName))
  54. {
  55. Console.WriteLine($"{@event.EventName.TrimStart('#')} - {@event.Participants.Count}");
  56. var participants = @event.Participants;
  57. foreach (var participant in participants.OrderBy(p => p))
  58. {
  59. Console.WriteLine($"{participant}");
  60. }
  61. }
  62. }
  63.  
  64. public class Event
  65. {
  66. public Event(int id, string eventName, List<string> participants)
  67. {
  68. Id = id;
  69. EventName = eventName;
  70. Participants = participants;
  71. }
  72.  
  73. public int Id { get; set; }
  74.  
  75. public string EventName { get; set; }
  76.  
  77. public List<string> Participants { get; set; }
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement