Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _06_User_logs
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text.RegularExpressions;
- class Program
- {
- static void Main()
- {
- // Judge LINK: https://judge.softuni.bg/Contests/Compete/Index/209#5
- var logsDict = new SortedDictionary<string, Dictionary<string, int>>();
- while (true)
- {
- string[] inputCommand = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- if (inputCommand[0] == "end")
- {
- break;
- }
- else
- {
- //string regexValidateIPv4 = @"'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'";
- string currentAddress = string.Join("", inputCommand[0]
- .ToCharArray()
- .Skip(3)
- .ToArray());
- string currentName = string.Join("", inputCommand[2]
- .ToCharArray()
- .Skip(5)
- .ToArray());
- bool isCurrentAddressValid = CheckAddress(currentAddress);
- // Holds the current log address
- var addressesLogs = new Dictionary<string, int>();
- if (isCurrentAddressValid == true)
- {
- addressesLogs.Add(currentAddress, 1);
- // If user is not present, add him.
- if (logsDict.ContainsKey(currentName) == false)
- {
- logsDict.Add(currentName, addressesLogs);
- }
- else
- {
- // Check if user has logged from different addresses in order to add them.
- if (logsDict[currentName].ContainsKey(currentAddress) == false)
- {
- logsDict[currentName].Add(currentAddress, 1);
- }
- // If not he has logged from previous IP.
- else
- {
- logsDict[currentName][currentAddress]++;
- }
- }
- }
- }
- }
- // PRINT
- foreach (var user in logsDict)
- {
- Console.WriteLine($"{user.Key}:");
- var addresses = user.Value;
- var lastAddress = addresses.Last();
- foreach (var address in addresses)
- {
- if (address.Equals(lastAddress))
- {
- Console.WriteLine($"{address.Key} => {address.Value}.");
- }
- else
- {
- Console.Write($"{address.Key} => {address.Value}, ");
- }
- }
- }
- }
- static bool CheckAddress(string address)
- {
- IPAddress validAddress;
- bool checkIfIPv6 = false;
- bool checkIfIPv4 = false;
- if (IPAddress.TryParse(address, out validAddress) == true)
- {
- checkIfIPv6 = validAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6;
- checkIfIPv4 = validAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;
- // This maybe pointless... If parse is successful better to return TRUE ?
- if (checkIfIPv4 == true || checkIfIPv6 == true)
- {
- return true;
- }
- return false;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement