Extract Combinations method to AdventOfCode.Extensions and refactor usages to improve modularity and reduce duplication
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AdventOfCode.Extensions;
|
||||
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static IEnumerable<TValue[]> Combinations<TValue>(this IEnumerable<TValue> values, int count)
|
||||
{
|
||||
var pool = values.ToArray();
|
||||
var poolLength = pool.Length;
|
||||
if (count > poolLength)
|
||||
yield break;
|
||||
var indices = Range(0, count).ToArray();
|
||||
yield return GetCombination(indices, pool);
|
||||
while (true)
|
||||
{
|
||||
var validIndex = false;
|
||||
var currentIndex = 0;
|
||||
foreach (var i in Range(0, count).Reverse())
|
||||
{
|
||||
currentIndex = i;
|
||||
if (indices[i] != i + poolLength - count)
|
||||
{
|
||||
validIndex = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!validIndex)
|
||||
yield break;
|
||||
|
||||
indices[currentIndex] += 1;
|
||||
foreach (var j in Range(currentIndex + 1, count))
|
||||
{
|
||||
Index ix = j - 1;
|
||||
indices[j] = indices[ix] + 1;
|
||||
}
|
||||
|
||||
yield return GetCombination(indices, pool);
|
||||
}
|
||||
|
||||
static TValue[] GetCombination(int[] innerIndices, TValue[] innerPool) =>
|
||||
innerIndices.Select(i => innerPool[i]).ToArray();
|
||||
|
||||
static IEnumerable<int> Range(int start, int end)
|
||||
{
|
||||
for (var i = start; i < end; i++)
|
||||
yield return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user