204 lines
8.5 KiB
C#
204 lines
8.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
|
|
namespace AdventOfCode.Extensions;
|
|
|
|
public static class EnumerableExtensions
|
|
{
|
|
/// <summary>
|
|
/// Generates all unique combinations of the specified length from the sequence, preserving the source order within each combination.
|
|
/// Each source element can be selected at most once in a generated combination.
|
|
/// If <paramref name="count"/> is greater than the number of elements in <paramref name="values"/>, no combinations are returned.
|
|
/// </summary>
|
|
/// <typeparam name="TValue">The type of elements in the source sequence.</typeparam>
|
|
/// <param name="values">The sequence whose elements are used to build the combinations.</param>
|
|
/// <param name="count">The length of each generated combination.</param>
|
|
/// <returns>A sequence of arrays containing the combinations from <paramref name="values"/>.</returns>
|
|
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;
|
|
int[] indices = [..Enumerable.Range(0, count)];
|
|
yield return GetCombination(indices, pool);
|
|
while (true)
|
|
{
|
|
var idx = count - 1;
|
|
for(;idx >= 0; --idx)
|
|
{
|
|
var maxPossibleIndex = idx + poolLength - count;
|
|
var isIndexBelowMax = indices[idx] != maxPossibleIndex;
|
|
if (isIndexBelowMax)
|
|
break;
|
|
}
|
|
if(idx < 0)
|
|
yield break;
|
|
|
|
indices[idx] += 1;
|
|
for (var j = idx + 1; j < indices.Length; j++)
|
|
{
|
|
indices[j] = indices[j - 1] + 1;
|
|
}
|
|
|
|
yield return GetCombination(indices, pool);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates combinations of the specified length from the sequence, allowing the same source element to be selected multiple times.
|
|
/// If <paramref name="count"/> is greater than the number of elements in <paramref name="values"/>, no combinations are returned.
|
|
/// </summary>
|
|
/// <typeparam name="TValue">The type of elements in the source sequence.</typeparam>
|
|
/// <param name="values">The sequence whose elements are used to build the repeated combinations.</param>
|
|
/// <param name="count">The length of each generated combination.</param>
|
|
/// <returns>A sequence of arrays containing the repeated combinations from <paramref name="values"/>.</returns>
|
|
public static IEnumerable<TValue[]> CombinationsWithRepeats<TValue>(this IEnumerable<TValue> values, int count)
|
|
{
|
|
var pool = values.ToArray();
|
|
var poolLength = pool.Length;
|
|
if (count > poolLength)
|
|
yield break;
|
|
int[] indices = [..Enumerable.Repeat(0, count)];
|
|
yield return GetCombination(indices, pool);
|
|
while (true)
|
|
{
|
|
var idx = count - 1;
|
|
for(;idx >= 0; --idx)
|
|
{
|
|
var maxPossibleIndex = poolLength - 1;
|
|
var isIndexBelowMax = indices[idx] != maxPossibleIndex;
|
|
if (isIndexBelowMax)
|
|
break;
|
|
}
|
|
if(idx < 0)
|
|
yield break;
|
|
|
|
var num = indices[idx];
|
|
for (var j = idx; j < indices.Length; j++)
|
|
{
|
|
indices[j] = num + 1;
|
|
}
|
|
|
|
yield return GetCombination(indices, pool);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates ordered permutations of the specified length from the sequence, permitting the same source element to appear multiple times in a selection.
|
|
/// If <paramref name="count"/> is greater than the number of elements in <paramref name="values"/>, no selections are returned.
|
|
/// </summary>
|
|
/// <typeparam name="TValue">The type of elements in the source sequence.</typeparam>
|
|
/// <param name="values">The sequence whose elements are used to build the repeated permutations.</param>
|
|
/// <param name="count">The length of each generated permutation.</param>
|
|
/// <returns>A sequence of arrays containing the ordered repeated permutations from <paramref name="values"/>.</returns>
|
|
public static IEnumerable<TValue[]> PermutationsWithRepeats<TValue>(this IEnumerable<TValue> values, int count)
|
|
{
|
|
var pool = values.ToArray();
|
|
if (count > pool.Length)
|
|
yield break;
|
|
var indices = Enumerable.Repeat(0, count).ToArray();
|
|
yield return GetCombination(indices, pool);
|
|
while (true)
|
|
{
|
|
var idx = count - 1;
|
|
for(;idx > 0; --idx)
|
|
{
|
|
var isIndexBelowMax = indices[idx] != idx + pool.Length - count;
|
|
if (isIndexBelowMax)
|
|
break;
|
|
}
|
|
if(indices.All(i => i >= pool.Length - 1))
|
|
yield break;
|
|
indices[idx] += 1;
|
|
for (var j = idx + 1; j < count; j++)
|
|
{
|
|
indices[j] = 0;
|
|
}
|
|
|
|
yield return GetCombination(indices, pool);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enumerates ordered permutations of the specified length from the sequence, without reusing the same source position within a permutation.
|
|
/// If <paramref name="count"/> is greater than the number of elements in <paramref name="values"/>, the returned sequence is empty.
|
|
/// </summary>
|
|
/// <typeparam name="TValue">The type of elements in the sequence.</typeparam>
|
|
/// <param name="values">The sequence from which to create permutations.</param>
|
|
/// <param name="count">The number of elements to include in each permutation.</param>
|
|
/// <returns>A sequence of arrays containing the ordered permutations from <paramref name="values"/>.</returns>
|
|
public static IEnumerable<TValue[]> Permutations<TValue>(this IEnumerable<TValue> values, int count)
|
|
{
|
|
var pool = values.ToArray();
|
|
var poolLength = pool.Length;
|
|
if (count > poolLength)
|
|
yield break;
|
|
var indices = Enumerable.Range(0, poolLength).ToArray();
|
|
var cycles = Enumerable.Range(0, poolLength + 1)
|
|
.Skip(poolLength - count + 1)
|
|
.Reverse()
|
|
.ToArray();
|
|
yield return GetCombination(indices[..count], pool);
|
|
var notDone = true;
|
|
while (notDone)
|
|
{
|
|
notDone = false;
|
|
for (var i = count - 1; i >= 0; i--)
|
|
{
|
|
cycles[i] -= 1;
|
|
if (cycles[i] == 0)
|
|
{
|
|
RotateIndices(indices, i);
|
|
cycles[i] = poolLength - i;
|
|
}
|
|
else
|
|
{
|
|
var j = cycles[i];
|
|
(indices[i], indices[^j]) = (indices[^j], indices[i]);
|
|
yield return GetCombination(indices[..count], pool);
|
|
notDone = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
yield break;
|
|
|
|
static void RotateIndices(int[] indices, int from)
|
|
{
|
|
var tmp = indices[from];
|
|
for (var j = from; j < indices.Length - 1; j++)
|
|
{
|
|
indices[j] = indices[j + 1];
|
|
}
|
|
indices[^1] = tmp;
|
|
}
|
|
}
|
|
|
|
private static TValue[] GetCombination<TValue>(int[] indices, TValue[] values) =>
|
|
indices.Select(i => values[i]).ToArray();
|
|
|
|
/// <summary>
|
|
/// Enumerates sliding windows over the sequence, yielding one array for each source element.
|
|
/// Each returned array contains the most recent values up to <paramref name="count"/>, so the initial windows may contain fewer than <paramref name="count"/> elements.
|
|
/// </summary>
|
|
/// <typeparam name="TValue">The type of elements in the sequence.</typeparam>
|
|
/// <param name="values">The sequence to enumerate.</param>
|
|
/// <param name="count">The maximum number of elements to include in each window.</param>
|
|
/// <returns>A sequence of arrays representing consecutive windows from <paramref name="values"/>.</returns>
|
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception>
|
|
public static IEnumerable<TValue[]> SlidingWindow<TValue>(this IEnumerable<TValue> values, int count)
|
|
{
|
|
var queue = new Queue<TValue>(count);
|
|
foreach (var value in values)
|
|
{
|
|
queue.Enqueue(value);
|
|
if(queue.Count > count)
|
|
queue.Dequeue();
|
|
yield return [.. queue];
|
|
}
|
|
}
|
|
} |