Advertisement
nikolayneykov

Untitled

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