Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _04.Events
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- public class EventsMain
- {
- public static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<Event> events = new List<Event>();
- Regex regex = new Regex(@"^\s*#([a-zA-z]+):\s*@([a-zA-Z]+)\s*([0-9]{1,2}):([0-9]{1,2})$");
- for (int i = 0; i < n; i++)
- {
- string inputInfo = Console.ReadLine();
- if (regex.IsMatch(inputInfo))
- {
- Match match = regex.Match(inputInfo);
- string location = match.Groups[2].Value;
- string personName = match.Groups[1].Value;
- int hours = int.Parse(match.Groups[3].Value);
- int minutes = int.Parse(match.Groups[4].Value);
- if (hours < 0 || hours> 23 || minutes < 0 || minutes > 59)
- {
- continue;
- }
- if (!events.Any(e => e.Person.Name == personName && e.Location == location))
- {
- var person = new Person(personName);
- person.Time.Add(new[] { hours, minutes });
- var @event = new Event(location, person);
- events.Add(@event);
- }
- else if (!events.Any(e => e.Person.Name == personName))
- {
- var person = new Person(personName);
- person.Time.Add(new[] { hours, minutes });
- var @event = new Event(location, person);
- events.Add(@event);
- }
- else
- {
- events.FirstOrDefault(e => e.Location == location && e.Person.Name == personName).Person.Time.Add(new[] { hours, minutes });
- }
- }
- }
- string[] locations = Console.ReadLine().Split(',');
- var sortedDic =
- events.Where(e => locations.Contains(e.Location)).OrderBy(e => e.Location).ThenBy(e => e.Person.Name);
- var grouped = sortedDic.GroupBy(e => e.Location);
- foreach (IGrouping<string, Event> grouping in grouped)
- {
- Console.WriteLine($"{grouping.Key}:");
- int i = 1;
- foreach (var @event in grouping)
- {
- Console.WriteLine($"{i}. {@event}");
- i++;
- }
- }
- }
- }
- public class Event
- {
- public Event(string location, Person person)
- {
- this.Location = location;
- this.Person = person;
- }
- public string Location { get; set; }
- public Person Person { get; set; }
- public override string ToString()
- {
- StringBuilder result = new StringBuilder();
- result.AppendFormat($"{this.Person}");
- return result.ToString();
- }
- }
- public class Person : IComparable<Person>
- {
- public Person(string name)
- {
- this.Name = name;
- this.Time = new List<int[]>();
- }
- public string Name { get; set; }
- public List<int[]> Time { get; set; }
- public int CompareTo(Person other)
- {
- return this.Name.CompareTo(other.Name);
- }
- public override string ToString()
- {
- List<string> times = new List<string>();
- this.Time = this.Time.OrderBy(t => t[0]).ThenBy(t => t[1]).ToList();
- for (int i = 0; i < Time.Count; i++)
- {
- times.Add($"{this.Time[i][0].ToString().PadLeft(2, '0')}:{this.Time[i][1].ToString().PadLeft(2, '0')}");
- }
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.AppendFormat($"{this.Name} -> {string.Join(", ",times)}");
- return stringBuilder.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement