Militsa

01. Anonymous Cache

Dec 8th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _01.Anonymous_Cache
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var data = new Dictionary<string,Dictionary<string,long>>();
  14.             var cache = new Dictionary<string, Dictionary<string, long>>();
  15.             var inputLine = string.Empty;
  16.  
  17.             while ((inputLine = Console.ReadLine()) != "thetinggoesskrra")
  18.             {
  19.                 string[] inputParameters = inputLine.Split(new []{' ', '-', '>', '|'}, StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 if (inputParameters.Length == 1)
  22.                 {
  23.                     string dataSet = inputParameters[0];
  24.  
  25.                     if (cache.ContainsKey(dataSet))
  26.                     {
  27.                         data[dataSet] = new Dictionary<string, long>(cache[dataSet]);
  28.                         cache.Remove(dataSet);
  29.                     }
  30.                     else
  31.                     {
  32.                         data[dataSet] = new Dictionary<string, long>();
  33.                     }
  34.                 }
  35.                 else
  36.                 {
  37.                     string dataKey = inputParameters[0];
  38.                     long dataSize = long.Parse(inputParameters[1]);
  39.                     string dataSet = inputParameters[2];
  40.  
  41.                     if (!data.ContainsKey(dataSet))
  42.                     {
  43.                         if (!cache.ContainsKey(dataSet))
  44.                         {
  45.                             cache[dataSet] = new Dictionary<string, long>();
  46.                         }
  47.  
  48.                         cache[dataSet][dataKey] = dataSize;
  49.                     }
  50.                      else
  51.                     {
  52.                         data[dataSet][dataKey] = dataSize;
  53.                     }
  54.                 }
  55.             }
  56.  
  57.             if (data.Count > 0)
  58.             {
  59.                 KeyValuePair<string, Dictionary<string, long>> result = data
  60.                     .OrderByDescending(ds => ds.Value.Sum(d => d.Value))
  61.                     .First();
  62.  
  63.                 Console.WriteLine("Data Set: {0}, Total Size: {1}", result.Key, result.Value.Sum(d => d.Value));
  64.  
  65.                 string prefix = "$.";
  66.  
  67.                 foreach (var value in result.Value)
  68.                 {
  69.                     Console.WriteLine("{0}{1}",prefix,value.Key);
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
Add Comment
Please, Sign In to add comment