RaceTimeParser.cs 941 B

1234567891011121314151617181920212223242526
  1. using System.Text.RegularExpressions;
  2. namespace Day6;
  3. public partial class RaceTimeParser
  4. {
  5. [GeneratedRegex(@"\d+")]
  6. private partial Regex NumberList();
  7. public IList<RaceStats> Parse(string inputFile)
  8. {
  9. using var reader = File.OpenText(inputFile);
  10. var timesLine = reader.ReadLine()!;
  11. var times = NumberList().Matches(timesLine.Substring("Time:".Length)).Cast<Match>().Select(x => long.Parse(x.Value)).ToList();
  12. var durationsLine = reader.ReadLine()!;
  13. var durations = NumberList().Matches(durationsLine.Substring("Distance:".Length)).Cast<Match>().Select(x => long.Parse(x.Value)).ToList();
  14. if (times.Count != durations.Count)
  15. {
  16. throw new Exception("Expecting the same number of times and distances");
  17. }
  18. return times.Zip(durations).Select(item => new RaceStats(item.First, item.Second)).ToList();
  19. }
  20. }