| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System;
- using System.IO;
- namespace D09._1
- {
- class Program
- {
- static bool TryGetToken(string input, ref int index, out int length, out int repetition)
- {
- length = 0;
- repetition = 0;
- int idx = index;
- if (input[idx++] != '(') return false;
- int nindex = idx;
- while (input[idx] >= '0' && input[idx] <= '9') idx++;
- length = int.Parse(input.Substring(nindex, idx - nindex));
- if (input[idx++] != 'x') return false;
- nindex = idx;
- while (input[idx] >= '0' && input[idx] <= '9') idx++;
- repetition = int.Parse(input.Substring(nindex, idx - nindex));
- if (input[idx++] != ')') return false;
- idx += length;
- index = idx - 1;
- length *= repetition;
- return true;
- }
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- int totalLength = 0;
- var text = File.ReadAllText(args[0]);
- for (var i = 0; i < text.Length; ++i)
- {
- if (text[i] == ' ' || text[i] == '\n' || text[i] == '\t') continue;
- if (TryGetToken(text, ref i, out int length, out int repetition))
- totalLength += length;
- else totalLength++;
- }
- Console.WriteLine(totalLength);
- }
- }
- }
|