52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
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;
|
|
for(var i = count - 1; i >= 0; i--)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
} |