JulianJulianov

13.RegularExpressionsMoreExercise - Santa's Secret Helper

May 26th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. 04. Santa's Secret Helper
  2. After the successful second Christmas, Santa needs to gather information about the behavior of children to plan the presents for next Christmas. He has a secret helper, who is sending him encrypted information. Your task is to decrypt it and create a list of the children who have been good.
  3. You will receive an integer, which represents a key and afterwards some messages, which you must decode by subtracting the key from the value of each character. After the decryption, to be considered a valid match, a message should:
  4. -   Have a name, which starts after '@' and contains only letters from the Latin alphabet
  5. -   Have a behaviour type - "G"(good) or "N"(naughty) and must be surrounded by "!" (exclamation mark).
  6. The order in the message should be: child’s name -> child’s behavior. They can be separated from the others by any character except: '@', '-', '!', ':' and '>'.
  7. You will be receiving message until you are given the “end” command. Afterwards, print the names of the children, who will receive a present, each on a new line.
  8. Input / Constraints
  9. • The first line holds n – the number which you have to subtract from the characters – integer in range [1…100];
  10. • On the next lines, you will be receiving encrypted messages.
  11. Output
  12. Print the names of the children, each on a new line
  13. Examples
  14. Input                       Output    Comments
  15. 3                           Kate      We receive three messages and to decrypt them we use the key:
  16. CNdwhamigyenumje$J$         Bobbie    First message has decryption key 3. So we substract from each characters code 3 and we receive:
  17. CEreelh-nmguuejn$J$                   @Kate^jfdvbkrjgb!G!
  18. CVwdq&gnmjkvng$Q$                     @Bobbie*kjdrrbgk!G!
  19. end                                   @Stan#dkjghskd!N!
  20.                                       They are all valid and they contain a child’s name and behavior – G for good and N for naughty.
  21.  
  22. Input                                     Output          Comments
  23. 3                                         Kim             We receive four messages. They are with key 3:
  24. N}eideidmk$'(mnyenmCNlpamnin$J$           Connor          Kzbfabfajh!$%jkvbkj@Kim^jkfk!G!
  25. ddddkkkkmvkvmCFrqqru-nvevek$J$nmgievnge   Valentine       aaaahhhhjshsj@Connor*ksbsbh!G!kjdfbskdb
  26. ppqmkkkmnolmnnCEhq/vkievk$Q$                              mmnjhhhjklijkk@Ben,shfbsh!N!
  27. yyegiivoguCYdohqwlqh/kguimhk$J$                           vvbdffsldr@Valentine,hdrfjeh!G!
  28. end
  29.  
  30. input                                                Output
  31. 4                                                    Bobbie
  32. ~lwzjkl~jenlymfDFsffmiCwozwlzjln%K%                  Sammie
  33. 0zfjrl}xnrlDWeqqmi/wnznlwzjnn%K%onhfhnf              Lana
  34. ;:<lyiljz{onzDPere=;=9<;8=rhknlf%K%                  Samantha
  35. Wonvfkmwzkmpwvzkm'lhjnlDWeqerxle0wlnzj{nz%K%nohwn
  36. DReh}e=<4lhzj1%K%
  37. end
  38.  
  39. using System;
  40. using System.Text.RegularExpressions;
  41. using System.Collections.Generic;
  42.  
  43. public class Program
  44. {
  45.     public static void Main()
  46.     {
  47.           var key = int.Parse(Console.ReadLine());
  48.  
  49.            var listOfTheChildrenWhoWillReceiveAPresent = new List<string>();
  50.  
  51.            var encryptedInfo = "";
  52.            while (!(encryptedInfo = Console.ReadLine()).Equals("end"))
  53.            {
  54.                var decryptedInfo = "";
  55.                foreach (var item in encryptedInfo)
  56.                {
  57.                    decryptedInfo += (char)(item - key);
  58.                }
  59.                var patternNameAndType = @"(?<=@)(?<Name>[A-Za-z]+)[^@\-!:>]*!(?<BehaviourType>G|N)(?=!)";
  60.                var matchNameAndBehaviourType = Regex.Matches(decryptedInfo, patternNameAndType);
  61.                foreach (Match item in matchNameAndBehaviourType)
  62.                {    
  63.                    var behaviourType = item.Groups["BehaviourType"].Value;
  64.                    if (behaviourType == "G")
  65.                    {
  66.                         var name = item.Groups["Name"].Value;
  67.                        listOfTheChildrenWhoWillReceiveAPresent.Add(name);
  68.                    }
  69.                }
  70.            }
  71.            Console.WriteLine(string.Join("\n", listOfTheChildrenWhoWillReceiveAPresent));
  72.     }
  73. }
Add Comment
Please, Sign In to add comment