Compare commits

...

3 Commits

19 changed files with 236 additions and 244 deletions

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -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;
}
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,25 @@
namespace AdventOfCode.HelperClasses;
public record Coordinate(long X, long Y)
{
public double GetEuclidianDistance(Coordinate other) =>
Math.Sqrt((X - other.X) * (X - other.X) + (Y - other.Y) * (Y - other.Y));
public static double GetEuclidianDistance(Coordinate a, Coordinate b) =>
Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
public static Coordinate operator +(Coordinate left, Coordinate right) =>
new(left.X + right.X, left.Y + right.Y);
}
public record Coordinate3d(long X, long Y, long Z)
{
public double GetEuclidianDistance(Coordinate3d other) =>
Math.Sqrt((X - other.X) * (X - other.X) + (Y - other.Y) * (Y - other.Y) + (Z - other.Z) * (Z - other.Z));
public static double GetEuclidianDistance(Coordinate3d a, Coordinate3d b) =>
Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y) + (a.Z - b.Z) * (a.Z - b.Z));
public static Coordinate3d operator +(Coordinate3d left, Coordinate3d right) =>
new(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}

View File

@ -0,0 +1,24 @@
namespace AdventOfCode.HelperClasses;
public class IntArrayEqualityComparer : IEqualityComparer<int[]>
{
public bool Equals(int[]? x, int[]? y)
{
if (x is null || y is null || x.Length != y.Length) return false;
return !x.Where((t, i) => t != y[i]).Any();
}
public int GetHashCode(int[] obj)
{
var result = 17;
foreach (var t in obj)
{
unchecked
{
result = result * 23 + t;
}
}
return result;
}
}

View File

@ -0,0 +1,28 @@
namespace AdventOfCode.HelperClasses;
public record IntegerRange(long start, long end)
{
public long Count() => end - start + 1;
public bool Contains(long number) => start <= number && end >= number;
public bool TryCombine(IntegerRange other, out IntegerRange combined)
{
if (Contains(other.start))
{
var isEndInRange = Contains(other.end);
combined = isEndInRange ? this : this with {end = other.end};
return true;
}
if (other.Contains(start))
{
var isEndInRange = other.Contains(end);
combined = isEndInRange ? other : other with {end = end};
return true;
}
combined = this;
return false;
}
}

View File

@ -10,6 +10,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AoC_2025.Runner", "AoC_2025
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdvenOfCode.Contracts", "AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj", "{0249A329-D5C1-4699-88BE-A668B362432E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.Extensions", "AdventOfCode.Extensions\AdventOfCode.Extensions.csproj", "{1466A0F6-7C88-47FF-B8F8-FF30368E26DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.HelperClasses", "AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj", "{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -32,6 +36,14 @@ Global
{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.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
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4F395AE3-DABB-4546-A4D0-5A62425FE168} = {05EBAA1C-B33B-4AFE-8635-A58D7CA509A7}

View File

@ -8,6 +8,8 @@
<ItemGroup>
<ProjectReference Include="..\AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj" />
<ProjectReference Include="..\AdventOfCode.Extensions\AdventOfCode.Extensions.csproj" />
<ProjectReference Include="..\AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -1,7 +1,4 @@
using System;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdvenOfCode.Contracts;
namespace AoC_2025;

View File

@ -1,7 +1,4 @@
using System;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdvenOfCode.Contracts;
namespace AoC_2025;

View File

@ -1,8 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using Coordinate = (int x, int y);
using AdvenOfCode.Contracts;
using AdventOfCode.HelperClasses;
namespace AoC_2025;
@ -64,7 +61,7 @@ public class Day04 : IPuzzleSolver<long>
{
var indexedCharacters = gridLine.Index();
var validRolls = indexedCharacters.Where(tuple => tuple.Item == '@');
var rollCoordinates = validRolls.Select(tuple => (xIndex, tuple.Index));
var rollCoordinates = validRolls.Select(tuple => new Coordinate(xIndex, tuple.Index));
return rollCoordinates;
}
@ -79,12 +76,12 @@ public class Day04 : IPuzzleSolver<long>
{
Coordinate[] neighbourDirections =
[
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1),
new(-1, -1), new(-1, 0), new(-1, 1),
new(0, -1), new(0, 1),
new(1, -1), new(1, 0), new(1, 1),
];
var possibleNeighbours = neighbourDirections
.Select(direction => (coordinate.x + direction.x, coordinate.y + direction.y));
.Select(direction => coordinate + direction);
var actualNeighbours = possibleNeighbours
.Where(coord => coordinates.Contains(coord))
.ToArray();

View File

@ -1,8 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using Range = (long from, long to);
using AdvenOfCode.Contracts;
using AdventOfCode.HelperClasses;
namespace AoC_2025;
@ -52,54 +49,49 @@ public class Day05 : IPuzzleSolver<long>
return parsedIds;
}
private Range[] ParseRanges(IEnumerable<string> ranges)
private IntegerRange[] ParseRanges(IEnumerable<string> ranges)
{
return ranges.Select(range =>
{
var split = range.Split('-', StringSplitOptions.RemoveEmptyEntries);
return (long.Parse(split[0]), long.Parse(split[1]));
return new IntegerRange(long.Parse(split[0]), long.Parse(split[1]));
})
.OrderBy(range => range.Item1)
.ThenBy(range => range.Item2)
.OrderBy(range => range.start)
.ThenBy(range => range.end)
.ToArray();
}
private IEnumerable<long> FilterFreshIds(IEnumerable<long> ids, Range[] freshIdRanges)
private IEnumerable<long> FilterFreshIds(IEnumerable<long> ids, IntegerRange[] freshIdRanges)
{
var freshIds = ids.Where(id => IsIdInRanges(id, freshIdRanges));
return freshIds;
}
private bool IsIdInRanges(long id, IEnumerable<Range> ranges)
private bool IsIdInRanges(long id, IEnumerable<IntegerRange> ranges)
{
return ranges.Any(range => IsIdInRange(id, range));
return ranges.Any(range => range.Contains(id));
}
private bool IsIdInRange(long id, Range range)
{
return range.from <= id && range.to >= id;
}
private long SumIdsInRanges(IEnumerable<Range> ranges)
private long SumIdsInRanges(IEnumerable<IntegerRange> ranges)
{
var sumFreshIds = 0L;
var highestSeenId = -1L;
foreach (var range in ranges)
{
var lowestNewId = Math.Max(highestSeenId + 1, range.from);
highestSeenId = Math.Max(highestSeenId, range.to);
var lowestNewId = Math.Max(highestSeenId + 1, range.start);
highestSeenId = Math.Max(highestSeenId, range.end);
sumFreshIds += Math.Max(0, highestSeenId - lowestNewId + 1);
}
return sumFreshIds;
}
private List<Range> CombineOverlappingRanges(Range[] ranges)
private List<IntegerRange> CombineOverlappingRanges(IntegerRange[] ranges)
{
var optimizedRanges = new List<Range>(ranges.Length) { ranges[0] };
var optimizedRanges = new List<IntegerRange>(ranges.Length) { ranges[0] };
foreach (var range in ranges)
{
if (TryCombineRanges(optimizedRanges[^1], range, out var combined))
if (optimizedRanges[^1].TryCombine(range, out var combined))
{
optimizedRanges[^1] = combined;
}
@ -112,28 +104,9 @@ public class Day05 : IPuzzleSolver<long>
return optimizedRanges;
}
private bool TryCombineRanges(Range first, Range second, out Range combined)
private static IEnumerable<long> CountIdsInRanges(IEnumerable<IntegerRange> combinedRanges)
{
if (IsIdInRange(second.from, first))
{
var isEndInRange = IsIdInRange(second.to, first);
combined = isEndInRange ? first : (first.from, second.to);
return true;
}
// if (IsIdInRange(first.from, second))
// {
// var isEndInRange = IsIdInRange(first.to, second);
// combined = isEndInRange ? second : (second.from, first.to);
// return true;
// }
combined = first;
return false;
}
private static IEnumerable<long> CountIdsInRanges(IEnumerable<Range> combinedRanges)
{
var countIdsInRanges = combinedRanges.Select(range => range.to - range.from + 1);
var countIdsInRanges = combinedRanges.Select(range => range.Count());
return countIdsInRanges;
}
}

View File

@ -1,8 +1,4 @@
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdvenOfCode.Contracts;
namespace AoC_2025;

View File

@ -1,9 +1,5 @@
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using Coordinate = (int x, int y);
using AdvenOfCode.Contracts;
using AdventOfCode.HelperClasses;
namespace AoC_2025;
@ -42,7 +38,7 @@ public class Day07 : IPuzzleSolver<long>
private Coordinate GetStart(string[] grid)
{
return (0, grid[0].IndexOf('S'));
return new Coordinate(0, grid[0].IndexOf('S'));
}
private long TraverseGridAndCountSplitters(string[] grid, Coordinate start)
@ -53,18 +49,18 @@ public class Day07 : IPuzzleSolver<long>
var splittersHit = 0L;
while (seen.TryDequeue(out var current))
{
if (current.x >= grid.Length) continue;
if (current.y >= grid[0].Length || current.y < 0) continue;
if (current.X >= grid.Length) continue;
if (current.Y >= grid[0].Length || current.Y < 0) continue;
if (grid[current.x][current.y] == '^')
if (grid[(int)current.X][(int)current.Y] == '^')
{
splittersHit++;
CheckAndEnqueueIfNotVisited((current.x, current.y - 1));
CheckAndEnqueueIfNotVisited((current.x, current.y + 1));
CheckAndEnqueueIfNotVisited(current with {Y = current.Y - 1});
CheckAndEnqueueIfNotVisited(current with {Y = current.Y + 1});
}
else
{
CheckAndEnqueueIfNotVisited((current.x + 1, current.y));
CheckAndEnqueueIfNotVisited(current with {X = current.X + 1});
}
}
@ -79,21 +75,21 @@ public class Day07 : IPuzzleSolver<long>
// Dictionary only for memoization
private long GetTimelinesCountRecursive(string[] grid, Coordinate current, Dictionary<Coordinate, long> memory)
{
if (current.x >= grid.Length)
if (current.X >= grid.Length)
{
return 1;
}
if(memory.TryGetValue(current, out var count)) return count;
if (grid[current.x][current.y] == '^')
if (grid[(int)current.X][(int)current.Y] == '^')
{
var resLeft = GetTimelinesCountRecursive(grid, (current.x, current.y - 1), memory);
var resRight = GetTimelinesCountRecursive(grid, (current.x, current.y + 1), memory);
var resLeft = GetTimelinesCountRecursive(grid, current with {Y = current.Y - 1}, memory);
var resRight = GetTimelinesCountRecursive(grid, current with {Y = current.Y + 1}, memory);
memory[current] = resLeft + resRight;
return resLeft + resRight;
}
var res = GetTimelinesCountRecursive(grid, (current.x + 1, current.y), memory);
var res = GetTimelinesCountRecursive(grid, current with {X = current.X + 1}, memory);
memory[current] = res;
return res;
}

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using Coordinate = (long x, long y, long z);
using AdvenOfCode.Contracts;
using AdventOfCode.Extensions;
using AdventOfCode.HelperClasses;
namespace AoC_2025;
@ -15,12 +11,12 @@ public class Day08 : IPuzzleSolver<long>
{
_amountToConnect = amountToConnect;
}
private Coordinate[] ParsePuzzleInput(string path)
private Coordinate3d[] ParsePuzzleInput(string path)
{
var puzzleInput = File.ReadAllLines(path)
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(line => line.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(str => long.Parse(str)).ToArray())
.Select(numbers => (numbers[0], numbers[1], numbers[2]))
.Select(numbers => new Coordinate3d(numbers[0], numbers[1], numbers[2]))
.ToArray();
return puzzleInput;
}
@ -41,16 +37,16 @@ public class Day08 : IPuzzleSolver<long>
var jBoxPairs = GetAllCombinationsSortedByDistance(jBoxes);
var circuits = CreateCircuits(jBoxes);
var lastConnected = LastCombinedConnectAllCircuits(circuits, jBoxPairs);
return lastConnected.a.x * lastConnected.b.x;
return lastConnected.a.X * lastConnected.b.X;
}
private List<HashSet<Coordinate>> CreateCircuits(IEnumerable<Coordinate> jBoxes)
private List<HashSet<Coordinate3d>> CreateCircuits(IEnumerable<Coordinate3d> jBoxes)
{
return jBoxes.Select(p => new HashSet<Coordinate> { p }).ToList();
return jBoxes.Select(p => new HashSet<Coordinate3d> { p }).ToList();
}
private List<HashSet<Coordinate>> CombineCircuits(List<HashSet<Coordinate>> circuits,
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs, int amountToConnect)
private List<HashSet<Coordinate3d>> CombineCircuits(List<HashSet<Coordinate3d>> circuits,
IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> jBoxPairs, int amountToConnect)
{
circuits = jBoxPairs
.Take(amountToConnect)
@ -58,8 +54,8 @@ public class Day08 : IPuzzleSolver<long>
return circuits;
}
private (Coordinate a, Coordinate b) LastCombinedConnectAllCircuits(List<HashSet<Coordinate>> circuits,
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs)
private (Coordinate3d a, Coordinate3d b) LastCombinedConnectAllCircuits(List<HashSet<Coordinate3d>> circuits,
IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> jBoxPairs)
{
foreach (var jBoxPair in jBoxPairs)
{
@ -67,15 +63,10 @@ public class Day08 : IPuzzleSolver<long>
if(circuits.Count <= 1)
return jBoxPair;
}
return ((0,0,0), (0,0,0));
return (new(0,0,0), new(0,0,0));
}
private long GetDistance(Coordinate a, Coordinate b)
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z);
}
private List<HashSet<Coordinate>> ConnectJBoxes(Coordinate a, Coordinate b, List<HashSet<Coordinate>> circuits)
private List<HashSet<Coordinate3d>> ConnectJBoxes(Coordinate3d a, Coordinate3d b, List<HashSet<Coordinate3d>> circuits)
{
var circuitToAddBTo = circuits.First(circuit => circuit.Contains(a));
var circuitToAddATo = circuits.First(circuit => circuit.Contains(b));
@ -90,25 +81,15 @@ public class Day08 : IPuzzleSolver<long>
return circuits;
}
private IEnumerable<(Coordinate boxA, Coordinate boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate> jBoxes)
private IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate3d> jBoxes)
{
var combinations = Combinations(jBoxes)
.OrderBy(x => GetDistance(x.a, x.b));
var combinations = jBoxes.Combinations(2)
.Select(x => (a: x[0], b: x[1]))
.OrderBy(x => Coordinate3d.GetEuclidianDistance(x.a, x.b));
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<Coordinate3d>> circuits, int amountMultiply)
{
return circuits
.Select(circuit => (long)circuit.Count)

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using Coordinate = (long x, long y);
using AdvenOfCode.Contracts;
using AdventOfCode.Extensions;
using AdventOfCode.HelperClasses;
using CoordinatePair = (long minX, long minY, long maxX, long maxY);
namespace AoC_2025;
@ -16,7 +12,7 @@ public class Day09 : IPuzzleSolver<long>
var puzzleInput = File.ReadAllLines(path)
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(line => line.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(str => long.Parse(str)).ToArray())
.Select(numbers => (numbers[0], numbers[1]))
.Select(numbers => new Coordinate(numbers[0], numbers[1]))
.ToArray();
return puzzleInput;
}
@ -42,7 +38,7 @@ public class Day09 : IPuzzleSolver<long>
private IOrderedEnumerable<long> GetOrderedAreas(IEnumerable<CoordinatePair> containedRectangles)
{
return containedRectangles
.Select(rectangle => GetArea((rectangle.maxX, rectangle.maxY), (rectangle.minX, rectangle.minY)))
.Select(rectangle => GetArea(new Coordinate(rectangle.maxX, rectangle.maxY), new Coordinate(rectangle.minX, rectangle.minY)))
.OrderDescending();
}
@ -54,7 +50,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)
@ -70,7 +66,7 @@ public class Day09 : IPuzzleSolver<long>
private CoordinatePair AsPair(Coordinate a, Coordinate b)
{
return (Math.Min(a.x, b.x), Math.Min(a.y, b.y), Math.Max(a.x, b.x), Math.Max(a.y, b.y));
return (Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Max(a.X, b.X), Math.Max(a.Y, b.Y));
}
private bool RectangleCrossesBorder(CoordinatePair rectangle, CoordinatePair borderSegment)
@ -81,18 +77,7 @@ public class Day09 : IPuzzleSolver<long>
private long GetArea(Coordinate tileA, Coordinate tileB)
{
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;
return (Math.Abs(tileA.X - tileB.X) + 1) * (Math.Abs(tileA.Y - tileB.Y) + 1);
}
private IEnumerable<(Coordinate a, Coordinate b)> GetBorders(Coordinate[] tiles)

View File

@ -1,18 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Data;
using AdvenOfCode.Contracts;
using AdventOfCode.Extensions;
using AdventOfCode.HelperClasses;
using Microsoft.Z3;
namespace AoC_2025;
public class Day10 : IPuzzleSolver<long>
{
private (uint lamps, uint[] buttons, int[] joltages)[] ParsePuzzleInput(string path)
private (uint lamps, uint[] buttons)[] ParsePuzzleInput(string path)
{
var puzzleInput = File.ReadAllLines(path)
.Where(line => !string.IsNullOrWhiteSpace(line))
@ -40,16 +36,8 @@ public class Day10 : IPuzzleSolver<long>
.Select(lampIndexes => lampIndexes.Aggregate((uint)0L, (acc, lampIndex) => acc | ((uint)1 << lampIndex)))
.ToArray())
.ToArray();
var joltages = puzzleInput
.Select(input => input.joltages)
.Select(joltage => joltage
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(jolt => int.Parse(jolt))
.ToArray())
.ToArray();
var res = lamps
.Zip(buttons, (lamp, button) => (lamp, button))
.Zip(joltages, (bl, joltage) => (bl.lamp, bl.button, joltage))
.ToArray();
return res;
}
@ -214,7 +202,7 @@ public class Day10 : IPuzzleSolver<long>
yield return ([..Enumerable.Repeat(0, patternLength)], 0);
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);
yield return (joltageLevels, buttonPattern.Length);
@ -271,71 +259,4 @@ public class Day10 : IPuzzleSolver<long>
var answer = answerExpression is IntNum number ? number.Int64 : 0;
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 bool Equals(int[]? x, int[]? y)
{
if (x is null || y is null || x.Length != y.Length) return false;
return !x.Where((t, i) => t != y[i]).Any();
}
public int GetHashCode(int[] obj)
{
var result = 17;
foreach (var t in obj)
{
unchecked
{
result = result * 23 + t;
}
}
return result;
}
}

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdvenOfCode.Contracts;
namespace AoC_2025;

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdvenOfCode.Contracts;
namespace AoC_2025;