Parser.cs 815 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.Text.RegularExpressions;
  2. namespace Day13;
  3. public partial class Parser
  4. {
  5. [GeneratedRegex(@".*")]
  6. private partial Regex LineMatch();
  7. public IEnumerable<Image> Parse(string inputFile)
  8. {
  9. using var reader = File.OpenText(inputFile);
  10. var buffer = new List<string>();
  11. while (!reader.EndOfStream)
  12. {
  13. var line = reader.ReadLine()!;
  14. if (string.IsNullOrEmpty(line) && buffer.Count > 0)
  15. {
  16. yield return new Image(buffer);
  17. buffer = new List<string>();
  18. }
  19. else
  20. {
  21. buffer.Add(line);
  22. }
  23. }
  24. if (buffer.Count > 0)
  25. {
  26. yield return new Image(buffer);
  27. }
  28. }
  29. }