Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class StartUp
- {
- static void Main()
- {
- Dictionary<string, List<string>> broadCasts = new Dictionary<string, List<string>>();
- Dictionary<string, List<string>> messages = new Dictionary<string, List<string>>();
- ReadInput(broadCasts,messages);
- PrintResult(broadCasts,messages);
- }
- public static void ReadInput(Dictionary<string,List<string>>broadCasts,Dictionary<string,List<string>>messages)
- {
- string input;
- while ((input = Console.ReadLine()) != "Hornet is Green")
- {
- if (input == null) continue;
- const string messagePattern = @"(^(\d+) \<\-\> ([a-zA-Z0-9]+))$";
- Regex message = new Regex(messagePattern);
- Match isMessage = message.Match(input);
- const string broadcastPattern = @"(\D+)\s(<->)\s([A-Za-z0-9]+)$";
- Regex broadcast = new Regex(broadcastPattern);
- Match isBroadcast = broadcast.Match(input);
- if (isMessage.Success)
- {
- GroupCollection messagePieces = isMessage.Groups;
- char[] personalCodeInput = messagePieces[2].Value.ToCharArray();
- Array.Reverse(personalCodeInput);
- string personalCode = string.Concat(personalCodeInput);
- string personalMessage = messagePieces[3].ToString();
- if (!messages.ContainsKey(personalCode))
- {
- messages.Add(personalCode, new List<string>());
- }
- messages[personalCode].Add(personalMessage);
- }
- if (isBroadcast.Success)
- {
- GroupCollection broadcastPieces = isBroadcast.Groups;
- string broadcastInfo = broadcastPieces[1].Value;
- char[] broadcastFrequency = broadcastPieces[3].Value.ToCharArray();
- for (int i = 0; i < broadcastFrequency.Length; i++)
- {
- char c = broadcastFrequency[i];
- if (char.IsLetter(c))
- {
- var c1 = c <= 122 && c >= 97
- ? char.ToUpper(c)
- : char.ToLower(c);
- broadcastFrequency[i] = c1;
- }
- }
- string frequency = string.Concat(broadcastFrequency);
- if (!broadCasts.ContainsKey(frequency))
- {
- broadCasts.Add(frequency, new List<string>());
- }
- broadCasts[frequency].Add(broadcastInfo);
- }
- }
- }
- public static void PrintResult(Dictionary<string,List<string>>broadCasts,Dictionary<string,List<string>>messages)
- {
- Console.WriteLine("Broadcasts:");
- if (!broadCasts.Any())
- {
- Console.WriteLine("None");
- }
- else
- {
- foreach (var br in broadCasts)
- {
- foreach (string m in br.Value)
- {
- Console.WriteLine($"{br.Key} -> {m}");
- }
- }
- }
- Console.WriteLine("Messages:");
- if (!messages.Any())
- {
- Console.WriteLine("None");
- }
- else
- {
- foreach (var mm in messages)
- {
- foreach (var t in mm.Value)
- {
- Console.WriteLine($"{mm.Key} -> {t}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement