Advertisement
WindFell

ForceBook

Mar 17th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class ForceBook
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         string input = Console.ReadLine();
  11.  
  12.         string pattern = @"(?<first>.+)(?<middle> \| | \-> )(?<second>.+)";
  13.         var forceBook = new Dictionary<string, List<string>>();
  14.  
  15.         while (input != "Lumpawaroo")
  16.         {
  17.             Match tokens = Regex.Match(input, pattern);
  18.  
  19.  
  20.             string delimiter = tokens.Groups["middle"].Value.Trim();
  21.             string forceUser = "";
  22.             string forceSide = "";
  23.  
  24.  
  25.             if (delimiter == "|")
  26.             {
  27.                 forceUser = tokens.Groups["second"].Value;
  28.                 forceSide = tokens.Groups["first"].Value;
  29.  
  30.                 if (forceBook.ContainsKey(forceSide) == false)
  31.                 {
  32.                     forceBook.Add(forceSide, new List<string>());
  33.                 }
  34.  
  35.                 bool userExists = false;
  36.  
  37.                 foreach (var pair in forceBook)
  38.                 {
  39.                     if (pair.Value.Contains(forceUser))
  40.                     {
  41.                         userExists = true;
  42.                     }
  43.                 }
  44.  
  45.                 if (userExists == false)
  46.                 {
  47.                     forceBook[forceSide].Add(forceUser);
  48.                 }
  49.             }
  50.             else if (delimiter == "->")
  51.             {
  52.                 forceUser = tokens.Groups["first"].Value;
  53.                 forceSide = tokens.Groups["second"].Value;
  54.  
  55.                 if (forceBook.ContainsKey(forceSide))
  56.                 {
  57.                     if (forceBook[forceSide].Contains(forceUser))
  58.                     {
  59.                         continue;
  60.                     }
  61.                 }
  62.  
  63.                 foreach (var record in forceBook)
  64.                 {
  65.                     if (record.Value.Contains(forceUser))
  66.                     {
  67.                         record.Value.Remove(forceUser);
  68.                     }
  69.                 }
  70.  
  71.                 if (forceBook.ContainsKey(forceSide) == false)
  72.                 {
  73.                     forceBook.Add(forceSide, new List<string>());
  74.                 }
  75.  
  76.                 forceBook[forceSide].Add(forceUser);
  77.                 Console.WriteLine($"{forceUser} joins the {forceSide} side!");
  78.             }
  79.  
  80.             input = Console.ReadLine();
  81.         }
  82.  
  83.         foreach (var record in forceBook.Where(f => f.Value.Count > 0).OrderByDescending(f => f.Value.Count).ThenBy(f => f.Key))
  84.         {
  85.             Console.WriteLine($"Side: {record.Key}, Members: {record.Value.Count}");
  86.  
  87.             foreach (string user in record.Value.OrderBy(u => u))
  88.             {
  89.                 Console.WriteLine($"! {user}");
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement