Advertisement
dimipan80

Activity Tracker

May 11th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. namespace _13.ActivityTracker
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     class ActivityTracker
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int linesCount = int.Parse(Console.ReadLine());
  12.  
  13.             SortedDictionary<int, SortedDictionary<string, int>> monthsActivity = new SortedDictionary<int, SortedDictionary<string, int>>();
  14.             string pattern = @"\d{2}/(\d{2})/\d{4}\s+(\w{1,20})\s+(\d+)";
  15.             for (int i = 0; i < linesCount; i++)
  16.             {
  17.                 string input = Console.ReadLine();
  18.                 MatchCollection matches = Regex.Matches(input, pattern);
  19.                 int month = 0, distance = 0;
  20.                 string username = string.Empty;
  21.                 foreach (Match match in matches)
  22.                 {
  23.                     month = int.Parse(match.Groups[1].Value);
  24.                     username = match.Groups[2].Value;
  25.                     distance = int.Parse(match.Groups[3].Value);
  26.                 }
  27.  
  28.                 if (!monthsActivity.ContainsKey(month))
  29.                 {
  30.                     monthsActivity[month] = new SortedDictionary<string, int>();
  31.                 }
  32.  
  33.                 if (!monthsActivity[month].ContainsKey(username))
  34.                 {
  35.                     monthsActivity[month][username] = 0;
  36.                 }
  37.  
  38.                 monthsActivity[month][username] += distance;
  39.             }
  40.  
  41.             foreach (var monthInfo in monthsActivity)
  42.             {
  43.                 Console.Write("{0}: ", monthInfo.Key);
  44.                 int count = monthInfo.Value.Count;
  45.                 foreach (var userInfo in monthInfo.Value)
  46.                 {
  47.                     count--;
  48.                     Console.Write("{0}({1})", userInfo.Key, userInfo.Value);
  49.                     if (count > 0)
  50.                     {
  51.                         Console.Write(", ");
  52.                     }
  53.                 }
  54.                 Console.WriteLine();
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement