ExpandResult Expand(string input, int startIndex, long length) { //WriteLine($"Expand(\"{input}\", {startIndex}, {length})"); ExpandResult result = new ExpandResult() { ExpandedLength = 0, ExpandedString = string.Empty }; int i = startIndex; while (i < startIndex + length) { if (input[i] == '(') { int start = i + 1; while (input[i] != ')') { i++; } int end = i; // ')' i++; string data = input.Substring(start, end - start); var split = data.Split('x'); long letterCount = long.Parse(split[0]); long repeatCount = long.Parse(split[1]); //WriteLine($" {i} / {letterCount}"); var innerResult = Expand(input, i, letterCount); for (long j = 0; j < repeatCount; j++) { result.ExpandedLength += innerResult.ExpandedLength; //result.ExpandedString += innerResult.ExpandedString; } i += (int)letterCount; } else { result.ExpandedLength++; //result.ExpandedString += input[i]; i++; } } return(result); }
public void Run(string[] args) { IReader reader; if (args.Length > 0) { reader = new FileReader(args[0]); } else { reader = new ConsoleReader(); } var input = reader.ReadLine(); ExpandResult result = Expand(input, 0, input.Length); //WriteLine($"{result.ExpandedString}: {result.ExpandedLength}"); WriteLine($"{result.ExpandedLength}"); }