| 1234567891011121314151617181920212223242526 |
- using System.Text.RegularExpressions;
- namespace Day6;
- public partial class RaceTimeParser
- {
- [GeneratedRegex(@"\d+")]
- private partial Regex NumberList();
-
- public IList<RaceStats> Parse(string inputFile)
- {
- using var reader = File.OpenText(inputFile);
- var timesLine = reader.ReadLine()!;
- var times = NumberList().Matches(timesLine.Substring("Time:".Length)).Cast<Match>().Select(x => long.Parse(x.Value)).ToList();
- var durationsLine = reader.ReadLine()!;
- var durations = NumberList().Matches(durationsLine.Substring("Distance:".Length)).Cast<Match>().Select(x => long.Parse(x.Value)).ToList();
- if (times.Count != durations.Count)
- {
- throw new Exception("Expecting the same number of times and distances");
- }
- return times.Zip(durations).Select(item => new RaceStats(item.First, item.Second)).ToList();
- }
- }
|