Extract Combinations method to AdventOfCode.Extensions and refactor usages to improve modularity and reduce duplication
This commit is contained in:
+2
-12
@@ -4,6 +4,7 @@ using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.Extensions;
|
||||
using Coordinate = (long x, long y);
|
||||
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)
|
||||
{
|
||||
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)
|
||||
@@ -84,17 +85,6 @@ public class Day09 : IPuzzleSolver<long>
|
||||
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)
|
||||
{
|
||||
return tiles.Zip([..tiles[1..], tiles[0]], (a, b) => (a, b));
|
||||
|
||||
Reference in New Issue
Block a user