From 56521d39d74a9e16d31facea8a3459dddf4159e2 Mon Sep 17 00:00:00 2001 From: Sebastian Lindemeier Date: Mon, 15 Dec 2025 10:44:38 +0100 Subject: [PATCH] Refactor `EnumerableExtensions` to simplify combination generation by removing `Range` method and streamlining index logic --- .../EnumerableExtensions.cs | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/AdventOfCode.Extensions/EnumerableExtensions.cs b/AdventOfCode.Extensions/EnumerableExtensions.cs index c7dd053..76244ee 100644 --- a/AdventOfCode.Extensions/EnumerableExtensions.cs +++ b/AdventOfCode.Extensions/EnumerableExtensions.cs @@ -12,29 +12,24 @@ public static class EnumerableExtensions var poolLength = pool.Length; if (count > poolLength) yield break; - var indices = Range(0, count).ToArray(); + var indices = Enumerable.Range(0, count).ToArray(); yield return GetCombination(indices, pool); while (true) { - var validIndex = false; - var currentIndex = 0; - for(var i = count - 1; i >= 0; i--) + int idx; + for(idx = count - 1; idx >= 0; idx--) { - currentIndex = i; - if (indices[i] != i + poolLength - count) - { - validIndex = true; + var isIndexBelowMax = indices[idx] != idx + poolLength - count; + if (isIndexBelowMax) break; - } } - if(!validIndex) + if(idx < 0) yield break; - indices[currentIndex] += 1; - foreach (var j in Range(currentIndex + 1, count)) + indices[idx] += 1; + for (var j = idx + 1; j < count; j++) { - Index ix = j - 1; - indices[j] = indices[ix] + 1; + indices[j] = indices[j - 1] + 1; } yield return GetCombination(indices, pool); @@ -42,11 +37,5 @@ public static class EnumerableExtensions static TValue[] GetCombination(int[] innerIndices, TValue[] innerPool) => innerIndices.Select(i => innerPool[i]).ToArray(); - - static IEnumerable Range(int start, int end) - { - for (var i = start; i < end; i++) - yield return i; - } } } \ No newline at end of file