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;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace _02.SpyGram
- {
- class Program
- {
- static void Main(string[] args)
- {
- Regex pattern = new Regex(@"TO:\s[A-Z]+;\sMESSAGE:\s[^\n]+;");
- Regex senderPattern = new Regex(@"TO:\s(?<recipient>[A-Z]+);\sMESSAGE:\s(?<message>[^\n;]+)");
- int privateKey = int.Parse(Console.ReadLine());
- string input = Console.ReadLine();
- List<int> digits = SplitDigits(privateKey);
- var messagesResult = new Dictionary<string, List<string>>();
- while (input != "END")
- {
- Match m = senderPattern.Match(input);
- string sender = m.Groups["recipient"].Value;
- if (pattern.IsMatch(input))
- {
- StringBuilder result = new StringBuilder("");
- int index = 0;
- foreach (char ch in input)
- {
- if (index == privateKey.ToString().Length)
- {
- index = 0;
- }
- int asciiNum = ch + digits[index];
- char newCh = (char)asciiNum;
- result.Append(newCh);
- index++;
- }
- if (!messagesResult.ContainsKey(sender))
- {
- messagesResult.Add(sender, new List<string>());
- }
- messagesResult[sender].Add(result.ToString());
- }
- input = Console.ReadLine();
- }
- foreach (var sms in messagesResult.OrderBy(s => s.Key))
- {
- Console.WriteLine(string.Join("\n", sms.Value));
- }
- }
- private static List<int> SplitDigits(int privateKey)
- {
- int value = privateKey;
- var digits = new List<int>();
- while (value > 0)
- {
- int digit = value % 10;
- digits.Add(digit);
- value /= 10;
- }
- digits.Reverse();
- return digits;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement