Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _02.SpyGram
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Regex pattern = new Regex(@"TO:\s[A-Z]+;\sMESSAGE:\s[^\n]+;");
  15.             Regex senderPattern = new Regex(@"TO:\s(?<recipient>[A-Z]+);\sMESSAGE:\s(?<message>[^\n;]+)");
  16.  
  17.             int privateKey = int.Parse(Console.ReadLine());
  18.             string input = Console.ReadLine();
  19.             List<int> digits = SplitDigits(privateKey);
  20.             var messagesResult = new Dictionary<string, List<string>>();
  21.            
  22.             while (input != "END")
  23.             {
  24.                 Match m = senderPattern.Match(input);
  25.                 string sender = m.Groups["recipient"].Value;
  26.  
  27.                 if (pattern.IsMatch(input))
  28.                 {
  29.                     StringBuilder result = new StringBuilder("");
  30.                     int index = 0;
  31.  
  32.                     foreach (char ch in input)
  33.                     {
  34.                         if (index == privateKey.ToString().Length)
  35.                         {
  36.                             index = 0;
  37.                         }
  38.                         int asciiNum = ch + digits[index];
  39.                         char newCh = (char)asciiNum;
  40.                         result.Append(newCh);
  41.                         index++;
  42.                     }
  43.  
  44.                     if (!messagesResult.ContainsKey(sender))
  45.                     {
  46.                         messagesResult.Add(sender, new List<string>());
  47.                     }
  48.                     messagesResult[sender].Add(result.ToString());
  49.                 }
  50.                
  51.                 input = Console.ReadLine();
  52.             }
  53.             foreach (var sms in messagesResult.OrderBy(s => s.Key))
  54.             {
  55.                 Console.WriteLine(string.Join("\n", sms.Value));
  56.             }
  57.         }
  58.  
  59.         private static List<int> SplitDigits(int privateKey)
  60.         {
  61.             int value = privateKey;
  62.             var digits = new List<int>();
  63.             while (value > 0)
  64.             {
  65.                 int digit = value % 10;
  66.                 digits.Add(digit);
  67.                 value /= 10;
  68.             }
  69.             digits.Reverse();
  70.             return digits;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement