| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System.Text.RegularExpressions;
- namespace Day3;
- public partial class SchematicParser
- {
- [GeneratedRegex(@"\d+|[^\d.]+")]
- private partial Regex TokenExtractor();
-
- public int Width { get; private set; }
- public int Height { get; private set; }
-
- public IEnumerable<Token> Parse(string inputFile)
- {
- using var reader = File.OpenText(inputFile);
- var row = 0;
- while (!reader.EndOfStream)
- {
- var line = reader.ReadLine()!;
- if (Width == 0)
- {
- Width = line.Length;
- }
- foreach (Match match in TokenExtractor().Matches(line))
- {
- if (match.Value[0] >= '0' && match.Value[0] <= '9')
- {
- yield return new NumberToken(match.Index, row, match.Value);
- }
- else
- {
- if (match.Length > 1)
- {
- throw new Exception($"Unexpected symbol token: {match.Value}");
- }
- yield return new SymbolToken(match.Index, row, match.Value);
- }
- }
- row++;
- }
- Height = row;
- }
- }
|