Advertisement
deleriumbg

Logs Agregator

Jun 3rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Logs_Aggregator
  6. {
  7.     class Program
  8.     {
  9.         public class User
  10.         {
  11.             public string Name { get; set; }
  12.             public HashSet<string> IPs { get; set; }
  13.             public int Duration { get; set; }
  14.         }
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             int numberOfLines = int.Parse(Console.ReadLine());
  19.             List<User> users = new List<User>();
  20.  
  21.             for (int i = 0; i < numberOfLines; i++)
  22.             {
  23.                 string[] input = Console.ReadLine().Split(' ');
  24.                 string ip = input[0];
  25.                 string userName = input[1];
  26.                 int duration = int.Parse(input[2]);
  27.  
  28.                 User user = new User();
  29.                 if (!users.Any(x => x.Name == userName))
  30.                 {
  31.                     user.Name = userName;
  32.                     user.IPs = new HashSet<string>();
  33.                     user.IPs.Add(ip);
  34.                     user.Duration += duration;
  35.                 }
  36.                 else
  37.                 {
  38.                     user = users.Find(x => x.Name == userName);
  39.                     user.IPs.Add(ip);
  40.                     user.Duration += duration;
  41.                 }
  42.                 users.Add(user);
  43.             }
  44.  
  45.             foreach (var user in users.Distinct().OrderBy(x => x.Name))
  46.             {
  47.                 Console.WriteLine($"{user.Name}: {user.Duration} [{string.Join(", ", user.IPs.OrderBy(x => x))}]");
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement