SimeonSheytanov

Roli Nested Dicts

Oct 26th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace RoliTheCoder
  6. {
  7.     public class RoliNested
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             Dictionary<int, Dictionary<string, List<string>>> data = ReadData();
  12.  
  13.             PrintOutput(data);
  14.         }
  15.  
  16.         private static void PrintOutput(Dictionary<int, Dictionary<string, List<string>>> data)
  17.         {
  18.             foreach (Dictionary<string, List<string>> dict in data.Values.OrderByDescending(ev => ev.Values.Sum(participants => participants.Count)).ThenBy(
  19.                 ev => ev.Keys.FirstOrDefault()))
  20.             {
  21.                 foreach (KeyValuePair<string, List<string>> ev in dict)
  22.                 {
  23.                     Console.WriteLine("{0} - {1}", ev.Key, ev.Value.Count);
  24.  
  25.                     foreach (string participant in ev.Value.OrderBy(part => part))
  26.                     {
  27.                         Console.WriteLine(participant);
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.  
  33.         private static Dictionary<int, Dictionary<string, List<string>>> ReadData()
  34.         {
  35.             string inputData = Console.ReadLine();
  36.  
  37.             Dictionary<int, Dictionary<string, List<string>>> data =
  38.                 new Dictionary<int, Dictionary<string, List<string>>>();
  39.  
  40.             while (inputData != "Time for Code")
  41.             {
  42.                 string[] commandInfo = inputData.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  43.  
  44.                 int id = int.Parse(commandInfo[0]);
  45.                 string eventName = commandInfo[1];
  46.                 List<string> participants = new List<string>(commandInfo.Skip(2));
  47.  
  48.                 if (eventName[0] != '#')
  49.                 {
  50.                     inputData = Console.ReadLine();
  51.                     continue;
  52.                 }
  53.  
  54.                 eventName = eventName.Substring(1);
  55.  
  56.                 if (data.ContainsKey(id))
  57.                 {
  58.                     if (data[id].ContainsKey(eventName))
  59.                     {
  60.                         data[id][eventName].AddRange(participants);
  61.                         data[id][eventName] = data[id][eventName].Distinct().ToList();
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     Dictionary<string, List<string>> info = new Dictionary<string, List<string>>
  67.                     {
  68.                         {eventName, participants.Distinct().ToList()}
  69.                     };
  70.                     data.Add(id, info);
  71.                 }
  72.  
  73.                 inputData = Console.ReadLine();
  74.             }
  75.  
  76.             return data;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment