SimeonSheytanov

Roli 2 Dicts

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