From e5c7063d5e56e9eed013a6285125ab1176b372b3 Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Sun, 15 Sep 2024 14:30:33 +0100 Subject: [PATCH] Use ReadOnlySpan.Split() (#557) - Treat some strings as spans and use their `Split()` method. - Use `string.Join(char)`. - Rename variable to theta symbol. --- src/ProjectEuler/Puzzles/Puzzle039.cs | 6 +++--- src/ProjectEuler/Puzzles/Puzzle042.cs | 13 ++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/ProjectEuler/Puzzles/Puzzle039.cs b/src/ProjectEuler/Puzzles/Puzzle039.cs index 4630edd9..2f9bb52d 100644 --- a/src/ProjectEuler/Puzzles/Puzzle039.cs +++ b/src/ProjectEuler/Puzzles/Puzzle039.cs @@ -32,9 +32,9 @@ internal static ICollection Solve(int perimeter) for (double b = 1; b < perimeter - a - 1; b++) { double tangent = a / b; - double theta = Math.Atan(tangent); + double θ = Math.Atan(tangent); - double c = a / Math.Sin(theta); + double c = a / Math.Sin(θ); if (Math.Abs(a + b + c - perimeterDouble) < double.Epsilon) { @@ -45,7 +45,7 @@ internal static ICollection Solve(int perimeter) c.ToString(Format, formatProvider), ]; - string solution = $"{{{string.Join(",", sides.Order())}}}"; + string solution = '{' + string.Join(',', sides.Order()) + '}'; solutions.Add(solution); } diff --git a/src/ProjectEuler/Puzzles/Puzzle042.cs b/src/ProjectEuler/Puzzles/Puzzle042.cs index ddf984fc..58fe7474 100644 --- a/src/ProjectEuler/Puzzles/Puzzle042.cs +++ b/src/ProjectEuler/Puzzles/Puzzle042.cs @@ -51,17 +51,16 @@ internal IList ReadWords() using var stream = ReadResource(); using var reader = new StreamReader(stream); - string rawWords = reader.ReadToEnd(); + var text = reader.ReadToEnd().AsSpan(); + var words = new List(); - string[] split = rawWords.Split(','); - - var words = new List(split.Length); - - foreach (string word in split) + foreach (var word in text.Split(',')) { - words.Add(word.Trim('\"')); + words.Add(text[word].Trim('\"').ToString()); } + words.TrimExcess(); + return words; }