Advertisement
Aliendreamer

hornet command

May 25th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class StartUp
  7. {
  8.     static void Main()
  9.     {
  10.  
  11.         Dictionary<string, List<string>> broadCasts = new Dictionary<string, List<string>>();
  12.         Dictionary<string, List<string>> messages = new Dictionary<string, List<string>>();
  13.        
  14.  
  15.        
  16.         ReadInput(broadCasts,messages);
  17.         PrintResult(broadCasts,messages);
  18.  
  19.     }
  20.  
  21.     public static void ReadInput(Dictionary<string,List<string>>broadCasts,Dictionary<string,List<string>>messages)
  22.     {
  23.         string input;
  24.         while ((input = Console.ReadLine()) != "Hornet is Green")
  25.         {
  26.             if (input == null) continue;
  27.  
  28.             const string messagePattern = @"(^(\d+) \<\-\> ([a-zA-Z0-9]+))$";
  29.             Regex message = new Regex(messagePattern);
  30.             Match isMessage = message.Match(input);
  31.  
  32.             const string broadcastPattern = @"(\D+)\s(<->)\s([A-Za-z0-9]+)$";
  33.             Regex broadcast = new Regex(broadcastPattern);
  34.             Match isBroadcast = broadcast.Match(input);
  35.            
  36.  
  37.             if (isMessage.Success)
  38.             {
  39.                 GroupCollection messagePieces = isMessage.Groups;
  40.                 char[] personalCodeInput = messagePieces[2].Value.ToCharArray();
  41.                 Array.Reverse(personalCodeInput);
  42.                 string personalCode = string.Concat(personalCodeInput);
  43.                 string personalMessage = messagePieces[3].ToString();
  44.                 if (!messages.ContainsKey(personalCode))
  45.                 {
  46.                     messages.Add(personalCode, new List<string>());
  47.                 }
  48.                 messages[personalCode].Add(personalMessage);
  49.  
  50.             }
  51.  
  52.             if (isBroadcast.Success)
  53.             {
  54.                 GroupCollection broadcastPieces = isBroadcast.Groups;
  55.                 string broadcastInfo = broadcastPieces[1].Value;
  56.                 char[] broadcastFrequency = broadcastPieces[3].Value.ToCharArray();
  57.                 for (int i = 0; i < broadcastFrequency.Length; i++)
  58.                 {
  59.                     char c = broadcastFrequency[i];
  60.                     if (char.IsLetter(c))
  61.                     {
  62.                         var c1 = c <= 122 && c >= 97
  63.                             ? char.ToUpper(c)
  64.                             : char.ToLower(c);
  65.                         broadcastFrequency[i] = c1;
  66.                     }
  67.                 }
  68.                 string frequency = string.Concat(broadcastFrequency);
  69.                 if (!broadCasts.ContainsKey(frequency))
  70.                 {
  71.                     broadCasts.Add(frequency, new List<string>());
  72.                 }
  73.                 broadCasts[frequency].Add(broadcastInfo);
  74.             }
  75.         }
  76.     }
  77.     public static void PrintResult(Dictionary<string,List<string>>broadCasts,Dictionary<string,List<string>>messages)
  78.     {
  79.         Console.WriteLine("Broadcasts:");
  80.         if (!broadCasts.Any())
  81.         {
  82.             Console.WriteLine("None");
  83.         }
  84.         else
  85.         {
  86.             foreach (var br in broadCasts)
  87.             {
  88.                 foreach (string m in br.Value)
  89.                 {
  90.                     Console.WriteLine($"{br.Key} -> {m}");
  91.                 }
  92.             }
  93.         }
  94.         Console.WriteLine("Messages:");
  95.         if (!messages.Any())
  96.         {
  97.             Console.WriteLine("None");
  98.         }
  99.         else
  100.         {
  101.             foreach (var mm in messages)
  102.             {
  103.                 foreach (var t in mm.Value)
  104.                 {
  105.                     Console.WriteLine($"{mm.Key} -> {t}");
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement