| 1234567891011121314151617181920212223242526272829303132 |
- using System.Text.RegularExpressions;
- namespace Day1;
- public partial class FilePipeline
- {
- private readonly string _fileName;
- private readonly IStringTransform[] _transforms;
- public FilePipeline(string fileName, params IStringTransform[] transforms)
- {
- _fileName = fileName;
- _transforms = transforms;
- }
- public IEnumerable<int> Extract()
- {
- using var reader = File.OpenText(_fileName);
- while (!reader.EndOfStream)
- {
- var line = reader.ReadLine();
- var result = _transforms.Aggregate(line!, (prev, transform) => transform.Transform(prev));
-
- if (result.Length < 1)
- {
- throw new InvalidOperationException($"Not enough digits: '{line}'");
- }
- yield return int.Parse(new string(new char[] { result.First(), result.Last() }));
- }
- }
- }
|