Эх сурвалжийг харах

Improved structure of solution code

Lukas Angerer 2 жил өмнө
parent
commit
ab10d499a0

+ 0 - 35
Day1/CalibrationExtractor.cs

@@ -1,35 +0,0 @@
-using System.Text.RegularExpressions;
-
-namespace Day1;
-
-public class CalibrationExtractor
-{
-    private readonly string _fileName;
-    private readonly DigitTransformer _transformer;
-    private readonly Regex _nonDigit = new Regex(@"[^\d]", RegexOptions.Compiled);
-
-    public CalibrationExtractor(string fileName, DigitTransformer transformer = null)
-    {
-        _fileName = fileName;
-        _transformer = transformer;
-    }
-
-    public IEnumerable<int> Extract()
-    {
-        using var reader = File.OpenText(_fileName);
-        while (!reader.EndOfStream)
-        {
-            var line = reader.ReadLine();
-            if (_transformer != null)
-            {
-                line = _transformer.Replace(line);
-            }
-            var calibrationValueStr = _nonDigit.Replace(line, "");
-            if (calibrationValueStr.Length < 1)
-            {
-                throw new InvalidOperationException($"Not enough digits: '{line}'");
-            }
-            yield return int.Parse(new string(new char[] { calibrationValueStr.First(), calibrationValueStr.Last() }));
-        }
-    }
-}

+ 3 - 3
Day1/Day1.csproj

@@ -8,13 +8,13 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <None Update="sample-input.txt">
+    <None Update="inputs\lan-input.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
-    <None Update="lan-input.txt">
+    <None Update="inputs\sample-input.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
-    <None Update="sample-input2.txt">
+    <None Update="inputs\sample-input2.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
   </ItemGroup>

+ 14 - 0
Day1/DigitExtractor.cs

@@ -0,0 +1,14 @@
+using System.Text.RegularExpressions;
+
+namespace Day1;
+
+public partial class DigitExtractor : IStringTransform
+{
+    [GeneratedRegex("[^\\d]", RegexOptions.IgnoreCase, "en-US")]
+    private static partial Regex NonDigit();
+    
+    public string Transform(string input)
+    {
+        return NonDigit().Replace(input, string.Empty);
+    }
+}

+ 0 - 45
Day1/DigitTransformer.cs

@@ -1,45 +0,0 @@
-using System.Text;
-
-namespace Day1;
-
-public class DigitTransformer
-{
-    private static readonly Dictionary<string, string> _digits = new Dictionary<string, string>
-    {
-        ["one"] = "1",
-        ["two"] = "2",
-        ["three"] = "3",
-        ["four"] = "4",
-        ["five"] = "5",
-        ["six"] = "6",
-        ["seven"] = "7",
-        ["eight"] = "8",
-        ["nine"] = "9",
-    };
-
-    public string Replace(string input)
-    {
-        var result = new StringBuilder();
-        for (var i = 0; i < input.Length; i++)
-        {
-            var current = input.Substring(i);
-            var next = false;
-            foreach (var kvp in _digits)
-            {
-                if (current.StartsWith(kvp.Key))
-                {
-                    result.Append(kvp.Value);
-                    next = true;
-                    break;
-                }
-            }
-
-            if (!next)
-            {
-                result.Append(current[0]);
-            }
-        }
-
-        return result.ToString();
-    }
-}

+ 32 - 0
Day1/FilePipeline.cs

@@ -0,0 +1,32 @@
+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() }));
+        }
+    }
+
+}

+ 6 - 0
Day1/IStringTransform.cs

@@ -0,0 +1,6 @@
+namespace Day1;
+
+public interface IStringTransform
+{
+    string Transform(string input);
+}

+ 5 - 9
Day1/Program.cs

@@ -1,22 +1,18 @@
 using Day1;
 
-var extractor = new CalibrationExtractor("./sample-input.txt");
+var extractor = new FilePipeline("./inputs/sample-input.txt", new DigitExtractor());
 Console.WriteLine("Sum is:");
 Console.WriteLine(extractor.Extract().Sum());
 
-extractor = new CalibrationExtractor("./lan-input.txt");
+extractor = new FilePipeline("./inputs/lan-input.txt", new DigitExtractor());
 Console.WriteLine("LAN sum is:");
 Console.WriteLine(extractor.Extract().Sum());
 
-extractor = new CalibrationExtractor("./sample-input2.txt", new DigitTransformer());
+extractor = new FilePipeline("./inputs/sample-input2.txt", new WordToDigitTransform(), new DigitExtractor());
 Console.WriteLine("Transformed sum is:");
-Console.WriteLine(extractor.Extract().Select(x =>
-{
-    Console.WriteLine(x);
-    return x;
-}).Sum());
+Console.WriteLine(extractor.Extract().Sum());
 
-extractor = new CalibrationExtractor("./lan-input.txt", new DigitTransformer());
+extractor = new FilePipeline("./inputs/lan-input.txt", new WordToDigitTransform(), new DigitExtractor());
 Console.WriteLine("LAN transformed sum is:");
 Console.WriteLine(extractor.Extract().Select(x =>
 {

+ 49 - 0
Day1/WordToDigitTransform.cs

@@ -0,0 +1,49 @@
+using System.Text.RegularExpressions;
+
+namespace Day1;
+
+public partial class WordToDigitTransform : IStringTransform
+{
+    
+    [GeneratedRegex("one|two|three|four|five|six|seven|eight|nine", RegexOptions.IgnoreCase, "en-US")]
+    private static partial Regex NumberWords();
+    
+    private static readonly Dictionary<string, char> Digits = new()
+    {
+        ["one"] = '1',
+        ["two"] = '2',
+        ["three"] = '3',
+        ["four"] = '4',
+        ["five"] = '5',
+        ["six"] = '6',
+        ["seven"] = '7',
+        ["eight"] = '8',
+        ["nine"] = '9',
+    };
+
+    public string Transform(string input)
+    {
+        var result = new List<char>();
+        for (var i = 0; i < input.Length; i++)
+        {
+            var current = input.Substring(i);
+            var next = false;
+            foreach (var kvp in Digits)
+            {
+                if (current.StartsWith(kvp.Key))
+                {
+                    result.Add(kvp.Value);
+                    next = true;
+                    break;
+                }
+            }
+
+            if (!next)
+            {
+                result.Add(current[0]);
+            }
+        }
+
+        return string.Concat(result);
+    }
+}

+ 0 - 0
Day1/lan-input.txt → Day1/inputs/lan-input.txt


+ 0 - 0
Day1/sample-input.txt → Day1/inputs/sample-input.txt


+ 0 - 0
Day1/sample-input2.txt → Day1/inputs/sample-input2.txt