Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace _03.Problem
- {
- public class ProblemThree
- {
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- StringBuilder cryptoBlockChain = new StringBuilder();
- for (int i = 0; i < n; i++)
- {
- string currentCryptoBlock = Console.ReadLine();
- cryptoBlockChain.Append(currentCryptoBlock);
- }
- var pattern = @"\[([^\d\[\]\{\}]*)([\d]{3,})([^\[\]\{\}]*)\]|\{([^\d\[\]\{\}]*)([\d]{3,})([^\[\]\{\}]*)\}";
- Regex regex = new Regex(pattern);
- MatchCollection matches = regex.Matches(cryptoBlockChain.ToString());
- StringBuilder result = new StringBuilder();
- foreach (Match match in matches)
- {
- if (match.ToString().StartsWith("{"))
- {
- int length = match.ToString().Length;
- string digits = match.Groups[5].Value.ToString();
- ParseNumbers(result, length, digits);
- }
- else if (match.ToString().StartsWith("["))
- {
- int length = match.ToString().Length;
- string digits = match.Groups[2].Value.ToString();
- ParseNumbers(result, length, digits);
- }
- }
- Console.WriteLine(result.ToString());
- }
- public static void ParseNumbers(StringBuilder result, int length, string digits)
- {
- if (digits.Length % 3 == 0)
- {
- while (digits.Length > 0)
- {
- string currentNumberAsString = new string(digits.Take(3).ToArray());
- StringBuilder sb = new StringBuilder(digits);
- sb.Remove(0, 3);
- digits = sb.ToString();
- int numberAsInt = int.Parse(currentNumberAsString);
- int charToAdd = numberAsInt - length;
- result.Append((char)charToAdd);
- }
- }
- }
- // my solution to compare:
- // int n = int.Parse(Console.ReadLine());
- // var sticked = new StringBuilder();
- // var output = new StringBuilder();
- // for (int i = 0; i < n; i++)
- // {
- // sticked.Append(Console.ReadLine());
- // }
- // var pattern = @"\[([^\d\[\]\{\}]*)(?<numbers>[\d]{3,})([^\[\]\{\}]*)\]|\{([^\d\[\]\{\}]*)(?<numbers>[\d]{3,})([^\[\]\{\}]*)\}";
- // Regex regex = new Regex(pattern);
- // MatchCollection cryptoBlocks = regex.Matches(sticked.ToString());
- // for (int i = 0; i < cryptoBlocks.Count; i++)
- // {
- // if (cryptoBlocks[i].Groups["numbers"].Value.Length % 3 != 0)
- // {
- // continue;
- // }
- // var currentBlock = cryptoBlocks[i].Groups["numbers"].Value;
- // var numbersLength = cryptoBlocks[i].Groups["numbers"].Value.Length;
- // var totalLength = cryptoBlocks[i].Length;
- // while (currentBlock.Length > 0)
- // {
- // var currentChar = currentBlock.Substring(0, 3);
- // output.Append((char)(int.Parse(currentChar) - totalLength));
- // currentBlock = currentBlock.Substring(3);
- // }
- // }
- // Console.WriteLine(output);
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement