Advertisement
Guest User

CryptoBlockchainEdited

a guest
Apr 19th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _03.Problem
  7. {
  8.     public class ProblemThree
  9.     {
  10.         public static void Main()
  11.         {
  12.            
  13.                 int n = int.Parse(Console.ReadLine());
  14.                 StringBuilder cryptoBlockChain = new StringBuilder();
  15.  
  16.                 for (int i = 0; i < n; i++)
  17.                 {
  18.                     string currentCryptoBlock = Console.ReadLine();
  19.                     cryptoBlockChain.Append(currentCryptoBlock);
  20.                 }
  21.  
  22.             var pattern = @"\[([^\d\[\]\{\}]*)([\d]{3,})([^\[\]\{\}]*)\]|\{([^\d\[\]\{\}]*)([\d]{3,})([^\[\]\{\}]*)\}";
  23.             Regex regex = new Regex(pattern);
  24.  
  25.             MatchCollection matches = regex.Matches(cryptoBlockChain.ToString());
  26.  
  27.             StringBuilder result = new StringBuilder();
  28.  
  29.                 foreach (Match match in matches)
  30.                 {
  31.                     if (match.ToString().StartsWith("{"))
  32.                     {
  33.                         int length = match.ToString().Length;
  34.                         string digits = match.Groups[5].Value.ToString();
  35.                         ParseNumbers(result, length, digits);
  36.                     }
  37.                     else if (match.ToString().StartsWith("["))
  38.                     {
  39.                         int length = match.ToString().Length;
  40.                         string digits = match.Groups[2].Value.ToString();
  41.                         ParseNumbers(result, length, digits);
  42.                     }
  43.                 }
  44.  
  45.                 Console.WriteLine(result.ToString());
  46.             }
  47.  
  48.             public static void ParseNumbers(StringBuilder result, int length, string digits)
  49.             {
  50.                 if (digits.Length % 3 == 0)
  51.                 {
  52.                     while (digits.Length > 0)
  53.                     {
  54.                         string currentNumberAsString = new string(digits.Take(3).ToArray());
  55.                         StringBuilder sb = new StringBuilder(digits);
  56.                         sb.Remove(0, 3);
  57.                         digits = sb.ToString();
  58.                         int numberAsInt = int.Parse(currentNumberAsString);
  59.                         int charToAdd = numberAsInt - length;
  60.                         result.Append((char)charToAdd);
  61.                     }
  62.                 }
  63.             }
  64.  
  65.  
  66.  
  67.             // my solution to compare:
  68.  
  69.             //    int n = int.Parse(Console.ReadLine());
  70.             //    var sticked = new StringBuilder();
  71.             //    var output = new StringBuilder();
  72.  
  73.             //    for (int i = 0; i < n; i++)
  74.             //    {
  75.             //        sticked.Append(Console.ReadLine());
  76.             //    }
  77.  
  78.             //    var pattern = @"\[([^\d\[\]\{\}]*)(?<numbers>[\d]{3,})([^\[\]\{\}]*)\]|\{([^\d\[\]\{\}]*)(?<numbers>[\d]{3,})([^\[\]\{\}]*)\}";
  79.             //    Regex regex = new Regex(pattern);
  80.  
  81.             //    MatchCollection cryptoBlocks = regex.Matches(sticked.ToString());
  82.  
  83.             //    for (int i = 0; i < cryptoBlocks.Count; i++)
  84.             //    {
  85.             //        if (cryptoBlocks[i].Groups["numbers"].Value.Length % 3 != 0)
  86.             //        {
  87.             //            continue;
  88.             //        }
  89.  
  90.             //        var currentBlock = cryptoBlocks[i].Groups["numbers"].Value;
  91.             //        var numbersLength = cryptoBlocks[i].Groups["numbers"].Value.Length;
  92.             //        var totalLength = cryptoBlocks[i].Length;
  93.  
  94.             //        while (currentBlock.Length > 0)
  95.             //        {
  96.             //            var currentChar = currentBlock.Substring(0, 3);
  97.             //            output.Append((char)(int.Parse(currentChar) - totalLength));
  98.             //            currentBlock = currentBlock.Substring(3);
  99.             //        }
  100.             //    }
  101.  
  102.             //    Console.WriteLine(output);
  103.             //}
  104.         }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement