Parser.cs 698 B

1234567891011121314151617181920212223242526
  1. using System.Text.RegularExpressions;
  2. namespace Day7;
  3. public partial class Parser
  4. {
  5. [GeneratedRegex(@"(\S+)\s+(\d+)")]
  6. private partial Regex HandMatch();
  7. public IEnumerable<Hand> Parse(string inputFile)
  8. {
  9. using var reader = File.OpenText(inputFile);
  10. while (!reader.EndOfStream)
  11. {
  12. var line = reader.ReadLine()!;
  13. var match = HandMatch().Match(line);
  14. if (!match.Success)
  15. {
  16. throw new Exception($"Expected hand and bid, but got: {line}");
  17. }
  18. yield return new Hand(match.Groups[1].Value, int.Parse(match.Groups[2].Value));
  19. }
  20. }
  21. }