Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace _04_Roli_The_Coder
- {
- public class RoliTheCoder
- {
- public static void Main()
- {
- var eventAndIdRegex = new Regex(@"^(?<id>\d+)\s+(?<event>#\w+)");
- var participantsRegex = new Regex(@"@(?<participant>[A-Za-z\d\-\']*)");
- List<Event> events = new List<Event>();
- while (true)
- {
- string line = Console.ReadLine();
- if (line == "Time for Code")
- break;
- var eventAndIdMatch = eventAndIdRegex.Match(line);
- if (!eventAndIdMatch.Success)
- continue;
- var eventId = int.Parse(eventAndIdMatch.Groups["id"].Value);
- var eventName = eventAndIdMatch.Groups["event"].Value;
- var participants = participantsRegex.Matches(line).Cast<Match>().Select(p => p.Value.Trim()).ToList();
- if (events.Any(e => e.Id == eventId))
- {
- var alreadyExistingEvent = events.First(e => e.Id == eventId);
- if (alreadyExistingEvent.EventName == eventName)
- alreadyExistingEvent.Participants.AddRange(participants);
- }
- else
- {
- var @event = new Event(eventId, eventName, new List<string>());
- @event.Participants.AddRange(participants);
- events.Add(@event);
- }
- }
- foreach (var @event in events)
- {
- @event.Participants = @event.Participants.Distinct().ToList();
- }
- //PRINT
- foreach (var @event in events.OrderByDescending(e => e.Participants.Count).ThenBy(e => e.EventName))
- {
- Console.WriteLine($"{@event.EventName.TrimStart('#')} - {@event.Participants.Count}");
- var participants = @event.Participants;
- foreach (var participant in participants.OrderBy(p => p))
- {
- Console.WriteLine($"{participant}");
- }
- }
- }
- public class Event
- {
- public Event(int id, string eventName, List<string> participants)
- {
- Id = id;
- EventName = eventName;
- Participants = participants;
- }
- public int Id { get; set; }
- public string EventName { get; set; }
- public List<string> Participants { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement