Extract `Combinations` method to `AdventOfCode.Extensions` and refactor usages to improve modularity and reduce duplication
This commit is contained in:
parent
d92293f631
commit
d0b612154a
|
|
@ -0,0 +1,9 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>disable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AoC_2025.Runner", "AoC_2025
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdvenOfCode.Contracts", "AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj", "{0249A329-D5C1-4699-88BE-A668B362432E}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdvenOfCode.Contracts", "AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj", "{0249A329-D5C1-4699-88BE-A668B362432E}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.Extensions", "AdventOfCode.Extensions\AdventOfCode.Extensions.csproj", "{1466A0F6-7C88-47FF-B8F8-FF30368E26DC}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
|
@ -32,6 +34,10 @@ Global
|
||||||
{0249A329-D5C1-4699-88BE-A668B362432E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0249A329-D5C1-4699-88BE-A668B362432E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{0249A329-D5C1-4699-88BE-A668B362432E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0249A329-D5C1-4699-88BE-A668B362432E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{0249A329-D5C1-4699-88BE-A668B362432E}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0249A329-D5C1-4699-88BE-A668B362432E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{1466A0F6-7C88-47FF-B8F8-FF30368E26DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{1466A0F6-7C88-47FF-B8F8-FF30368E26DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{1466A0F6-7C88-47FF-B8F8-FF30368E26DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{1466A0F6-7C88-47FF-B8F8-FF30368E26DC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{4F395AE3-DABB-4546-A4D0-5A62425FE168} = {05EBAA1C-B33B-4AFE-8635-A58D7CA509A7}
|
{4F395AE3-DABB-4546-A4D0-5A62425FE168} = {05EBAA1C-B33B-4AFE-8635-A58D7CA509A7}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj" />
|
<ProjectReference Include="..\AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj" />
|
||||||
|
<ProjectReference Include="..\AdventOfCode.Extensions\AdventOfCode.Extensions.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using System.Data.Common;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using AdvenOfCode.Contracts;
|
using AdvenOfCode.Contracts;
|
||||||
|
using AdventOfCode.Extensions;
|
||||||
using Coordinate = (long x, long y, long z);
|
using Coordinate = (long x, long y, long z);
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
@ -92,21 +93,11 @@ public class Day08 : IPuzzleSolver<long>
|
||||||
|
|
||||||
private IEnumerable<(Coordinate boxA, Coordinate boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate> jBoxes)
|
private IEnumerable<(Coordinate boxA, Coordinate boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate> jBoxes)
|
||||||
{
|
{
|
||||||
var combinations = Combinations(jBoxes)
|
var combinations = jBoxes.Combinations(2)
|
||||||
|
.Select(x => (a: x[0], b: x[1]))
|
||||||
.OrderBy(x => GetDistance(x.a, x.b));
|
.OrderBy(x => GetDistance(x.a, x.b));
|
||||||
return combinations;
|
return combinations;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<(TValue a, TValue b)> Combinations<TValue>(IEnumerable<TValue> values)
|
|
||||||
{
|
|
||||||
var enumeratedValues = values.Index().ToArray();
|
|
||||||
var pairs =
|
|
||||||
from a in enumeratedValues
|
|
||||||
from b in enumeratedValues
|
|
||||||
where a.Index < b.Index
|
|
||||||
select (a.Item, b.Item);
|
|
||||||
return pairs;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long MultiplyLargestCircuitLength(List<HashSet<Coordinate>> circuits, int amountMultiply)
|
private long MultiplyLargestCircuitLength(List<HashSet<Coordinate>> circuits, int amountMultiply)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using System.Data.Common;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using AdvenOfCode.Contracts;
|
using AdvenOfCode.Contracts;
|
||||||
|
using AdventOfCode.Extensions;
|
||||||
using Coordinate = (long x, long y);
|
using Coordinate = (long x, long y);
|
||||||
using CoordinatePair = (long minX, long minY, long maxX, long maxY);
|
using CoordinatePair = (long minX, long minY, long maxX, long maxY);
|
||||||
|
|
||||||
|
|
@ -54,7 +55,7 @@ public class Day09 : IPuzzleSolver<long>
|
||||||
|
|
||||||
private IEnumerable<CoordinatePair> GetAllPossibleRectanglesAsPairs(Coordinate[] redTiles)
|
private IEnumerable<CoordinatePair> GetAllPossibleRectanglesAsPairs(Coordinate[] redTiles)
|
||||||
{
|
{
|
||||||
return Combinations(redTiles).Select(x => AsPair(x.a, x.b));
|
return redTiles.Combinations(2).Select(x => AsPair(x[0], x[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
private CoordinatePair[] GetBordersAsPairs(Coordinate[] redTiles)
|
private CoordinatePair[] GetBordersAsPairs(Coordinate[] redTiles)
|
||||||
|
|
@ -84,17 +85,6 @@ public class Day09 : IPuzzleSolver<long>
|
||||||
return (Math.Abs(tileA.x - tileB.x) + 1) * (Math.Abs(tileA.y - tileB.y) + 1);
|
return (Math.Abs(tileA.x - tileB.x) + 1) * (Math.Abs(tileA.y - tileB.y) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<(TValue a, TValue b)> Combinations<TValue>(IEnumerable<TValue> values)
|
|
||||||
{
|
|
||||||
var enumeratedValues = values.Index().ToArray();
|
|
||||||
var pairs =
|
|
||||||
from a in enumeratedValues
|
|
||||||
from b in enumeratedValues
|
|
||||||
where a.Index < b.Index
|
|
||||||
select (a.Item, b.Item);
|
|
||||||
return pairs;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<(Coordinate a, Coordinate b)> GetBorders(Coordinate[] tiles)
|
private IEnumerable<(Coordinate a, Coordinate b)> GetBorders(Coordinate[] tiles)
|
||||||
{
|
{
|
||||||
return tiles.Zip([..tiles[1..], tiles[0]], (a, b) => (a, b));
|
return tiles.Zip([..tiles[1..], tiles[0]], (a, b) => (a, b));
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Data.Common;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using AdvenOfCode.Contracts;
|
using AdvenOfCode.Contracts;
|
||||||
|
using AdventOfCode.Extensions;
|
||||||
using Microsoft.Z3;
|
using Microsoft.Z3;
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
@ -214,7 +215,7 @@ public class Day10 : IPuzzleSolver<long>
|
||||||
yield return ([..Enumerable.Repeat(0, patternLength)], 0);
|
yield return ([..Enumerable.Repeat(0, patternLength)], 0);
|
||||||
for (var i = 1; i <= buttons.Length; i++)
|
for (var i = 1; i <= buttons.Length; i++)
|
||||||
{
|
{
|
||||||
foreach (var buttonPattern in Combinations(buttons, i))
|
foreach (var buttonPattern in buttons.Combinations(i))
|
||||||
{
|
{
|
||||||
var joltageLevels = GetJoltageLevelsForButtonPattern(patternLength, buttonPattern);
|
var joltageLevels = GetJoltageLevelsForButtonPattern(patternLength, buttonPattern);
|
||||||
yield return (joltageLevels, buttonPattern.Length);
|
yield return (joltageLevels, buttonPattern.Length);
|
||||||
|
|
@ -271,50 +272,6 @@ public class Day10 : IPuzzleSolver<long>
|
||||||
var answer = answerExpression is IntNum number ? number.Int64 : 0;
|
var answer = answerExpression is IntNum number ? number.Int64 : 0;
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<TValue[]> Combinations<TValue>(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class IntArrayEqualityComparer : IEqualityComparer<int[]>
|
public class IntArrayEqualityComparer : IEqualityComparer<int[]>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue