Advertisement
mastersan12

ForceBook

Apr 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P09.ForceBook
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var dictionary = new Dictionary<string, List<string>>();
  12.             var forceUsers = new List<string>();
  13.  
  14.             string command = "";
  15.             while ((command = Console.ReadLine()) != "Lumpawaroo")
  16.             {
  17.                 if (command.IndexOf(" | ") >= 0)
  18.                 {
  19.                     string[] forceInputs = command.Split(" | ");
  20.                     string forceSide = forceInputs[0];
  21.                     string forceUser = forceInputs[1];
  22.  
  23.                     if (!forceUsers.Contains(forceUser))
  24.                     {
  25.                         if (!dictionary.ContainsKey(forceSide))
  26.                         {
  27.                             dictionary.Add(forceSide, new List<string>()
  28.                         {forceUser});
  29.                         }
  30.                         else
  31.                         {
  32.                             dictionary[forceSide].Add(forceUser);
  33.                         }
  34.                     }
  35.                     forceUsers.Add(forceUser);
  36.                 }
  37.                 else
  38.                 {
  39.                     string[] forceInputs = command.Split(" -> ");
  40.                     string forceUser = forceInputs[0];
  41.                     string forceSide = forceInputs[1];
  42.  
  43.                     if (forceUsers.Contains(forceUser))
  44.                     {
  45.                         string userCurrentSide = "";
  46.                         foreach (var key in dictionary.Keys)
  47.                         {
  48.                             if (dictionary[key].Contains(forceUser))
  49.                             {
  50.                                 userCurrentSide = key;
  51.                                 break;
  52.                             }
  53.                         }
  54.  
  55.                         if (!dictionary.ContainsKey(forceSide))
  56.                         {
  57.                             dictionary[forceSide] = new List<string>();
  58.                         }
  59.                         dictionary[userCurrentSide].Remove(forceUser);
  60.                         dictionary[forceSide].Add(forceUser);
  61.                     }
  62.                     else
  63.                     {
  64.                         if (!dictionary.ContainsKey(forceSide))
  65.                         {
  66.                             dictionary.Add(forceSide, new List<string>()
  67.                             { forceUser });
  68.                         }
  69.                         else
  70.                         {
  71.                             dictionary[forceSide].Add(forceUser);
  72.                         }
  73.                     }
  74.                     Console.WriteLine($"{forceUser} joins the {forceSide} side!");
  75.                     if (!forceUsers.Contains(forceUser))
  76.                     {
  77.                         forceUsers.Add(forceUser);
  78.                     }
  79.                 }
  80.             }
  81.             foreach (var item in dictionary.OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key))
  82.             {
  83.                 string forceSide = item.Key;
  84.                 var members = item.Value;
  85.                 if (members.Count > 0)
  86.                 {
  87.                     Console.WriteLine($"Side: {forceSide}, Members: {members.Count}");
  88.                     foreach (var member in members.OrderBy(x => x))
  89.                     {
  90.                         Console.WriteLine($"! {member}");
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement