WindFell

ForceBook

Mar 16th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 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 //90%
  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.                 if (forceBook[forceSide].Contains(forceUser) == false)
  36.                 {
  37.                     forceBook[forceSide].Add(forceUser);
  38.                 }
  39.             }
  40.             else if (delimiter == "->")
  41.             {
  42.                 forceUser = tokens.Groups["first"].Value;
  43.                 forceSide = tokens.Groups["second"].Value;
  44.  
  45.                 foreach (var record in forceBook)
  46.                 {
  47.                     if (record.Value.Contains(forceUser))
  48.                     {
  49.                         record.Value.Remove(forceUser);
  50.                     }
  51.                 }
  52.  
  53.                 if (forceBook.ContainsKey(forceSide) == false)
  54.                 {
  55.                     forceBook.Add(forceSide, new List<string>());
  56.                 }
  57.  
  58.                 forceBook[forceSide].Add(forceUser);
  59.                 Console.WriteLine($"{forceUser} joins the {forceSide} side!");
  60.             }
  61.  
  62.             input = Console.ReadLine();
  63.         }
  64.  
  65.         foreach (var record in forceBook.Where(f => f.Value.Count > 0).OrderByDescending(f => f.Value.Count).ThenBy(f => f.Key))
  66.         {
  67.             Console.WriteLine($"Side: {record.Key}, Members: {record.Value.Count}");
  68.  
  69.             foreach (string user in record.Value.OrderBy(u => u))
  70.             {
  71.                 Console.WriteLine($"! {user}");
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment