Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace Emoji_Sumator
- {
- class Program
- {
- static void Main()
- {
- //lookahead и lookbehind ?= и ?<=
- string pattern = @"(?<=\s)(?<emoji>:(?<name>[a-z]{4,}):)(?=[\s.,!\?])";
- //the text to dig for emojis
- string textToProcess = Console.ReadLine();
- MatchCollection emojis = Regex.Matches(textToProcess, pattern);
- string emojiCode = Console.ReadLine();
- //get the codes for the letters of the encrypted emoji
- int[] codes = emojiCode.Split(":").Select(int.Parse).ToArray();
- List<string> emojiNames = new List<string>();
- //return the emoji in string form
- string decryptedEmoji = GetEmojiCodeDecrypted(codes);
- int sumPowerEmojis = 0;
- //need this in the end to print emojis if they exist
- bool doesExist = false;
- //need this to know if I have to multiply the power by 2
- bool isDecryptedEmoji = false;
- if (emojis.Count > 0)
- {
- doesExist = true;
- foreach (Match one in emojis)
- {
- //in group "name" is just the string of letters
- string emoji = one.Groups["name"].Value;
- //compare clean emoji without ::
- if (decryptedEmoji == emoji) isDecryptedEmoji = true;
- //add the emojis with the ::, so can be printed in the end appropriately
- //in group "emoji" is the :string:
- emojiNames.Add(one.Groups["emoji"].Value);
- //sum the ascii codes
- sumPowerEmojis += SumEmojisPower(emoji);
- }
- }
- if (isDecryptedEmoji)
- {
- sumPowerEmojis *= 2;
- }
- PrintResult(doesExist, emojiNames, sumPowerEmojis);
- }
- static int SumEmojisPower(string emoji)
- {
- int sum = 0;
- for (int i = 0; i < emoji.Length; i++)
- {
- sum += emoji[i];
- }
- return sum;
- }
- static string GetEmojiCodeDecrypted(int[] code)
- {
- string decryptedEmoji = "";
- for (int i = 0; i < code.Length; i++)
- {
- decryptedEmoji += (char)code[i];
- }
- return decryptedEmoji;
- }
- static void PrintResult(bool foundEmoji, List<string> emojis, int sum)
- {
- if (foundEmoji)
- {
- Console.Write($"Emojis found: {string.Join(", ", emojis)}{Environment.NewLine}");
- }
- Console.WriteLine($"Total Emoji Power: {sum}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement