Advertisement
Guest User

Logs Aggregator

a guest
Oct 3rd, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. class UserLogs
  7. {
  8. static void Main()
  9. {
  10. int lines = int.Parse(Console.ReadLine());
  11. StringBuilder iPLogs = new StringBuilder();
  12. SortedDictionary<string, SortedDictionary<string, int>> output = new SortedDictionary<string, SortedDictionary<string, int>>();
  13.  
  14. for (int i = 0; i < lines; i++)
  15. {
  16. string inputLine = Console.ReadLine();
  17. iPLogs.Append(inputLine);
  18. iPLogs.Append("\n");
  19. }
  20.  
  21. Regex regex = new Regex(@"(.+) (.+) (\d+)");
  22. MatchCollection matches = regex.Matches(iPLogs.ToString());
  23.  
  24. foreach (Match match in matches)
  25. {
  26. SortedDictionary<string, int> subDictionary = new SortedDictionary<string, int>();
  27.  
  28. string iP = match.Groups[1].ToString();
  29. string user = match.Groups[2].ToString();
  30. int duration = int.Parse(match.Groups[3].ToString());
  31.  
  32. if (output.ContainsKey(user))
  33. {
  34. subDictionary = output[user];
  35. if (subDictionary.ContainsKey(iP))
  36. {
  37. int repetitions = subDictionary[iP] + duration;
  38. subDictionary[iP] = repetitions;
  39. }
  40. else
  41. {
  42. subDictionary.Add(iP, duration);
  43. }
  44.  
  45. output[user] = subDictionary;
  46. }
  47. else
  48. {
  49. subDictionary.Add(iP, duration);
  50. output.Add(user, subDictionary);
  51. }
  52. }
  53.  
  54. foreach (var subDictionary in output)
  55. {
  56. Console.Write("{0}: ", subDictionary.Key);
  57.  
  58. int totalDuration = 0;
  59. foreach (var time in subDictionary.Value)
  60. {
  61. totalDuration += time.Value;
  62. }
  63.  
  64. Console.Write("{0} [", totalDuration);
  65. int counter = 1;
  66. foreach (var ip in subDictionary.Value)
  67. {
  68. Console.Write("{0}", ip.Key);
  69. if (counter == subDictionary.Value.Count)
  70. {
  71. Console.WriteLine("]");
  72. }
  73. else
  74. {
  75. Console.Write(", ");
  76. }
  77. counter++;
  78. }
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement