Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. string input = System.IO.File.ReadAllText("input.txt");
  10.  
  11. ulong result = Decompress(input);
  12.  
  13. Console.WriteLine(result);
  14. }
  15.  
  16. private static ulong Decompress(string input)
  17. {
  18. ulong result = 0;
  19.  
  20. for(int i = 0; i < input.Length; i++)
  21. {
  22. char character = input[i];
  23.  
  24. if (character == '(')
  25. {
  26. string current = input.Substring(i);
  27. int indexOfClosingBracket = current.IndexOf(')');
  28. string[] instruction = current.Substring(0, indexOfClosingBracket + 1).Split('x');
  29.  
  30. int numberOfCharacters = int.Parse(instruction[0].Substring(1));
  31. int numberOfTimes = int.Parse(instruction[1].TrimEnd(')'));
  32.  
  33. //Get the next X number of characters.
  34. string data = current.Substring(indexOfClosingBracket + 1, numberOfCharacters);
  35.  
  36. string decompressed = string.Empty;
  37.  
  38. //Repeat this string Y number of times.
  39. for (int x = 0; x < numberOfTimes; x++)
  40. decompressed += data;
  41.  
  42. result += (ulong)decompressed.Length;
  43.  
  44. //Skip to the end of the instruction.
  45. i += indexOfClosingBracket + numberOfCharacters;
  46. }
  47. else
  48. result++;
  49. }
  50.  
  51. return result;
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement