Deserboy

Untitled

Aug 4th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace ConsoleApp4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int capacity = int.Parse(Console.ReadLine());
  14.             string input = Console.ReadLine();
  15.             Dictionary<string, int> sentMessages = new Dictionary<string, int>();
  16.             Dictionary<string, int> recievedMessages = new Dictionary<string, int>();
  17.             Dictionary<string, int> finalSum = new Dictionary<string, int>();
  18.             while (input != "Statistics")
  19.             {
  20.                 string[] tokens = input.Split('=').ToArray();
  21.                 if (tokens[0] == "Add")
  22.                 {
  23.                     if (sentMessages.ContainsKey(tokens[0]) && recievedMessages.ContainsKey(tokens[0]))
  24.                     {
  25.                         continue;
  26.                     }
  27.                     else
  28.                     {
  29.                         int sent = int.Parse(tokens[2]);
  30.                         int recieved = int.Parse(tokens[3]);
  31.                         sentMessages.Add(tokens[1], sent);
  32.                         recievedMessages.Add(tokens[1], recieved);
  33.                         finalSum.Add(tokens[1], sent + recieved);
  34.                     }
  35.                 }
  36.                 else if (tokens[0] == "Message")
  37.                 {
  38.                     if (sentMessages.ContainsKey(tokens[1]) && recievedMessages.ContainsKey(tokens[2]))
  39.                     {
  40.                         sentMessages[tokens[1]]++;
  41.                         recievedMessages[tokens[2]]++;
  42.                         finalSum[tokens[1]]++;
  43.                         finalSum[tokens[2]]++;
  44.                         if (sentMessages[tokens[1]] == capacity)
  45.                         {
  46.                             Console.WriteLine($"{tokens[1]} reached the capacity!");
  47.                             sentMessages.Remove(tokens[1]);
  48.                             finalSum.Remove(tokens[1]);
  49.                         }
  50.                         else if (recievedMessages[tokens[2]] == capacity)
  51.                         {
  52.                             Console.WriteLine($"{tokens[2]} reached the capacity!");
  53.                             recievedMessages.Remove(tokens[2]);
  54.                             finalSum.Remove(tokens[2]);
  55.                         }
  56.                     }
  57.                 }
  58.                 else if (tokens[0] == "Empty")
  59.                 {
  60.                     if (tokens[1] == "All")
  61.                     {
  62.                         sentMessages.Clear();
  63.                         recievedMessages.Clear();
  64.                         finalSum.Clear();
  65.                     }
  66.                     else
  67.                     {
  68.                         sentMessages.Remove(tokens[1]);
  69.                         recievedMessages.Remove(tokens[1]);
  70.                         finalSum.Remove(tokens[1]);
  71.                     }
  72.                 }
  73.                 input = Console.ReadLine();
  74.             }
  75.             Console.WriteLine($"Users count: {finalSum.Count}");
  76.             foreach(var user in finalSum.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  77.             {
  78.                 Console.WriteLine($"{user.Key} - {user.Value}");
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment