Advertisement
yanass

Emoji Sumator Regex

Aug 2nd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Emoji_Sumator
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             //lookahead и lookbehind ?=  и ?<=
  13.             string pattern = @"(?<=\s)(?<emoji>:(?<name>[a-z]{4,}):)(?=[\s.,!\?])";
  14.  
  15.             //the text to dig for emojis
  16.             string textToProcess = Console.ReadLine();
  17.  
  18.             MatchCollection emojis = Regex.Matches(textToProcess, pattern);
  19.  
  20.             string emojiCode = Console.ReadLine();
  21.  
  22.             //get the codes for the letters of the encrypted emoji
  23.             int[] codes = emojiCode.Split(":").Select(int.Parse).ToArray();
  24.  
  25.             List<string> emojiNames = new List<string>();
  26.  
  27.             //return the emoji in string form
  28.             string decryptedEmoji = GetEmojiCodeDecrypted(codes);
  29.  
  30.             int sumPowerEmojis = 0;
  31.  
  32.             //need this in the end to print emojis if they exist
  33.             bool doesExist = false;
  34.  
  35.             //need this to know if I have to multiply the power by 2
  36.             bool isDecryptedEmoji = false;
  37.  
  38.             if (emojis.Count > 0)
  39.             {
  40.                 doesExist = true;
  41.  
  42.                 foreach (Match one in emojis)
  43.                 {
  44.                     //in group "name" is just the string of letters
  45.                     string emoji = one.Groups["name"].Value;
  46.  
  47.                     //compare clean emoji without ::
  48.                     if (decryptedEmoji == emoji) isDecryptedEmoji = true;
  49.  
  50.                     //add the emojis with the ::, so can be printed in the end appropriately
  51.                     //in group "emoji" is the :string:
  52.                     emojiNames.Add(one.Groups["emoji"].Value);
  53.  
  54.                     //sum the ascii codes
  55.                     sumPowerEmojis += SumEmojisPower(emoji);
  56.                 }
  57.             }
  58.  
  59.             if (isDecryptedEmoji)
  60.             {
  61.                 sumPowerEmojis *= 2;
  62.             }
  63.  
  64.             PrintResult(doesExist, emojiNames, sumPowerEmojis);
  65.  
  66.         }
  67.  
  68.         static int SumEmojisPower(string emoji)
  69.         {
  70.             int sum = 0;
  71.             for (int i = 0; i < emoji.Length; i++)
  72.             {
  73.                 sum += emoji[i];
  74.             }
  75.  
  76.             return sum;
  77.         }
  78.  
  79.         static string GetEmojiCodeDecrypted(int[] code)
  80.         {
  81.             string decryptedEmoji = "";
  82.  
  83.             for (int i = 0; i < code.Length; i++)
  84.             {
  85.                 decryptedEmoji += (char)code[i];
  86.  
  87.             }
  88.             return decryptedEmoji;
  89.         }
  90.  
  91.         static void PrintResult(bool foundEmoji, List<string> emojis, int sum)
  92.         {
  93.             if (foundEmoji)
  94.             {
  95.                 Console.Write($"Emojis found: {string.Join(", ", emojis)}{Environment.NewLine}");
  96.             }
  97.  
  98.             Console.WriteLine($"Total Emoji Power: {sum}");
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement