Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApplication
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- string input = System.IO.File.ReadAllText("input.txt");
- ulong result = Decompress(input);
- Console.WriteLine(result);
- }
- private static ulong Decompress(string input)
- {
- ulong result = 0;
- for(int i = 0; i < input.Length; i++)
- {
- char character = input[i];
- if (character == '(')
- {
- string current = input.Substring(i);
- int indexOfClosingBracket = current.IndexOf(')');
- string[] instruction = current.Substring(0, indexOfClosingBracket + 1).Split('x');
- int numberOfCharacters = int.Parse(instruction[0].Substring(1));
- int numberOfTimes = int.Parse(instruction[1].TrimEnd(')'));
- //Get the next X number of characters.
- string data = current.Substring(indexOfClosingBracket + 1, numberOfCharacters);
- string decompressed = string.Empty;
- //Repeat this string Y number of times.
- for (int x = 0; x < numberOfTimes; x++)
- decompressed += data;
- result += (ulong)decompressed.Length;
- //Skip to the end of the instruction.
- i += indexOfClosingBracket + numberOfCharacters;
- }
- else
- result++;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement