Advertisement
Gesh4o

Events

Feb 29th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. namespace _04.Events
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8.  
  9. public class EventsMain
  10. {
  11. public static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14.  
  15. List<Event> events = new List<Event>();
  16.  
  17. Regex regex = new Regex(@"^\s*#([a-zA-z]+):\s*@([a-zA-Z]+)\s*([0-9]{1,2}):([0-9]{1,2})$");
  18.  
  19.  
  20. for (int i = 0; i < n; i++)
  21. {
  22. string inputInfo = Console.ReadLine();
  23. if (regex.IsMatch(inputInfo))
  24. {
  25. Match match = regex.Match(inputInfo);
  26.  
  27. string location = match.Groups[2].Value;
  28. string personName = match.Groups[1].Value;
  29. int hours = int.Parse(match.Groups[3].Value);
  30. int minutes = int.Parse(match.Groups[4].Value);
  31.  
  32. if (hours < 0 || hours> 23 || minutes < 0 || minutes > 59)
  33. {
  34. continue;
  35. }
  36.  
  37. if (!events.Any(e => e.Person.Name == personName && e.Location == location))
  38. {
  39. var person = new Person(personName);
  40. person.Time.Add(new[] { hours, minutes });
  41. var @event = new Event(location, person);
  42. events.Add(@event);
  43. }
  44. else if (!events.Any(e => e.Person.Name == personName))
  45. {
  46. var person = new Person(personName);
  47. person.Time.Add(new[] { hours, minutes });
  48. var @event = new Event(location, person);
  49. events.Add(@event);
  50. }
  51. else
  52. {
  53. events.FirstOrDefault(e => e.Location == location && e.Person.Name == personName).Person.Time.Add(new[] { hours, minutes });
  54. }
  55. }
  56. }
  57.  
  58. string[] locations = Console.ReadLine().Split(',');
  59.  
  60. var sortedDic =
  61. events.Where(e => locations.Contains(e.Location)).OrderBy(e => e.Location).ThenBy(e => e.Person.Name);
  62.  
  63.  
  64. var grouped = sortedDic.GroupBy(e => e.Location);
  65.  
  66. foreach (IGrouping<string, Event> grouping in grouped)
  67. {
  68. Console.WriteLine($"{grouping.Key}:");
  69. int i = 1;
  70. foreach (var @event in grouping)
  71. {
  72. Console.WriteLine($"{i}. {@event}");
  73. i++;
  74. }
  75. }
  76. }
  77. }
  78.  
  79. public class Event
  80. {
  81. public Event(string location, Person person)
  82. {
  83. this.Location = location;
  84. this.Person = person;
  85. }
  86.  
  87. public string Location { get; set; }
  88.  
  89. public Person Person { get; set; }
  90.  
  91. public override string ToString()
  92. {
  93. StringBuilder result = new StringBuilder();
  94.  
  95. result.AppendFormat($"{this.Person}");
  96.  
  97. return result.ToString();
  98. }
  99. }
  100.  
  101. public class Person : IComparable<Person>
  102. {
  103. public Person(string name)
  104. {
  105. this.Name = name;
  106. this.Time = new List<int[]>();
  107. }
  108.  
  109. public string Name { get; set; }
  110.  
  111. public List<int[]> Time { get; set; }
  112.  
  113. public int CompareTo(Person other)
  114. {
  115. return this.Name.CompareTo(other.Name);
  116. }
  117.  
  118. public override string ToString()
  119. {
  120. List<string> times = new List<string>();
  121. this.Time = this.Time.OrderBy(t => t[0]).ThenBy(t => t[1]).ToList();
  122. for (int i = 0; i < Time.Count; i++)
  123. {
  124. times.Add($"{this.Time[i][0].ToString().PadLeft(2, '0')}:{this.Time[i][1].ToString().PadLeft(2, '0')}");
  125. }
  126. StringBuilder stringBuilder = new StringBuilder();
  127. stringBuilder.AppendFormat($"{this.Name} -> {string.Join(", ",times)}");
  128.  
  129. return stringBuilder.ToString();
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement