| 12345678910111213141516171819202122232425262728293031 |
- using System.Drawing;
- using System.Text.RegularExpressions;
- namespace Day18;
- public partial class Parser
- {
- [GeneratedRegex(@"([UDLR])\s+(\d+)\s+\((.*)\)")]
- private partial Regex LineMatch();
-
- public IEnumerable<Instruction> Parse(string inputFile)
- {
- using var reader = File.OpenText(inputFile);
- var colorConverter = new ColorConverter();
-
- while (!reader.EndOfStream)
- {
- var line = reader.ReadLine()!;
- var match = LineMatch().Match(line);
- if (!match.Success)
- {
- throw new Exception($"Invalid instruction: {line}");
- }
- yield return new Instruction(
- match.Groups[1].Value[0],
- int.Parse(match.Groups[2].Value),
- (Color)colorConverter.ConvertFromString(match.Groups[3].Value)!);
- }
- }
- }
|