Compare commits
No commits in common. "aaf32260d0af4ce870a4f1aaee1d9c6c28594be7" and "d92293f631f0deb55367031e816acac27885226a" have entirely different histories.
aaf32260d0
...
d92293f631
|
|
@ -1,9 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
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);
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,10 +10,6 @@ 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
|
||||
|
|
@ -36,14 +32,6 @@ 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}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,6 @@
|
|||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj" />
|
||||
<ProjectReference Include="..\AdventOfCode.Extensions\AdventOfCode.Extensions.csproj" />
|
||||
<ProjectReference Include="..\AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.HelperClasses;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
using Coordinate = (int x, int y);
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
@ -61,7 +64,7 @@ public class Day04 : IPuzzleSolver<long>
|
|||
{
|
||||
var indexedCharacters = gridLine.Index();
|
||||
var validRolls = indexedCharacters.Where(tuple => tuple.Item == '@');
|
||||
var rollCoordinates = validRolls.Select(tuple => new Coordinate(xIndex, tuple.Index));
|
||||
var rollCoordinates = validRolls.Select(tuple => (xIndex, tuple.Index));
|
||||
return rollCoordinates;
|
||||
}
|
||||
|
||||
|
|
@ -76,12 +79,12 @@ public class Day04 : IPuzzleSolver<long>
|
|||
{
|
||||
Coordinate[] neighbourDirections =
|
||||
[
|
||||
new(-1, -1), new(-1, 0), new(-1, 1),
|
||||
new(0, -1), new(0, 1),
|
||||
new(1, -1), new(1, 0), new(1, 1),
|
||||
(-1, -1), (-1, 0), (-1, 1),
|
||||
(0, -1), (0, 1),
|
||||
(1, -1), (1, 0), (1, 1),
|
||||
];
|
||||
var possibleNeighbours = neighbourDirections
|
||||
.Select(direction => coordinate + direction);
|
||||
.Select(direction => (coordinate.x + direction.x, coordinate.y + direction.y));
|
||||
var actualNeighbours = possibleNeighbours
|
||||
.Where(coord => coordinates.Contains(coord))
|
||||
.ToArray();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.HelperClasses;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
using Range = (long from, long to);
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
@ -49,49 +52,54 @@ public class Day05 : IPuzzleSolver<long>
|
|||
return parsedIds;
|
||||
}
|
||||
|
||||
private IntegerRange[] ParseRanges(IEnumerable<string> ranges)
|
||||
private Range[] ParseRanges(IEnumerable<string> ranges)
|
||||
{
|
||||
return ranges.Select(range =>
|
||||
{
|
||||
var split = range.Split('-', StringSplitOptions.RemoveEmptyEntries);
|
||||
return new IntegerRange(long.Parse(split[0]), long.Parse(split[1]));
|
||||
return (long.Parse(split[0]), long.Parse(split[1]));
|
||||
})
|
||||
.OrderBy(range => range.start)
|
||||
.ThenBy(range => range.end)
|
||||
.OrderBy(range => range.Item1)
|
||||
.ThenBy(range => range.Item2)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private IEnumerable<long> FilterFreshIds(IEnumerable<long> ids, IntegerRange[] freshIdRanges)
|
||||
private IEnumerable<long> FilterFreshIds(IEnumerable<long> ids, Range[] freshIdRanges)
|
||||
{
|
||||
var freshIds = ids.Where(id => IsIdInRanges(id, freshIdRanges));
|
||||
return freshIds;
|
||||
}
|
||||
|
||||
private bool IsIdInRanges(long id, IEnumerable<IntegerRange> ranges)
|
||||
private bool IsIdInRanges(long id, IEnumerable<Range> ranges)
|
||||
{
|
||||
return ranges.Any(range => range.Contains(id));
|
||||
return ranges.Any(range => IsIdInRange(id, range));
|
||||
}
|
||||
|
||||
private bool IsIdInRange(long id, Range range)
|
||||
{
|
||||
return range.from <= id && range.to >= id;
|
||||
}
|
||||
|
||||
private long SumIdsInRanges(IEnumerable<IntegerRange> ranges)
|
||||
private long SumIdsInRanges(IEnumerable<Range> ranges)
|
||||
{
|
||||
var sumFreshIds = 0L;
|
||||
var highestSeenId = -1L;
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
var lowestNewId = Math.Max(highestSeenId + 1, range.start);
|
||||
highestSeenId = Math.Max(highestSeenId, range.end);
|
||||
var lowestNewId = Math.Max(highestSeenId + 1, range.from);
|
||||
highestSeenId = Math.Max(highestSeenId, range.to);
|
||||
sumFreshIds += Math.Max(0, highestSeenId - lowestNewId + 1);
|
||||
}
|
||||
|
||||
return sumFreshIds;
|
||||
}
|
||||
|
||||
private List<IntegerRange> CombineOverlappingRanges(IntegerRange[] ranges)
|
||||
private List<Range> CombineOverlappingRanges(Range[] ranges)
|
||||
{
|
||||
var optimizedRanges = new List<IntegerRange>(ranges.Length) { ranges[0] };
|
||||
var optimizedRanges = new List<Range>(ranges.Length) { ranges[0] };
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
if (optimizedRanges[^1].TryCombine(range, out var combined))
|
||||
if (TryCombineRanges(optimizedRanges[^1], range, out var combined))
|
||||
{
|
||||
optimizedRanges[^1] = combined;
|
||||
}
|
||||
|
|
@ -103,10 +111,29 @@ public class Day05 : IPuzzleSolver<long>
|
|||
|
||||
return optimizedRanges;
|
||||
}
|
||||
|
||||
private static IEnumerable<long> CountIdsInRanges(IEnumerable<IntegerRange> combinedRanges)
|
||||
|
||||
private bool TryCombineRanges(Range first, Range second, out Range combined)
|
||||
{
|
||||
var countIdsInRanges = combinedRanges.Select(range => range.Count());
|
||||
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);
|
||||
return countIdsInRanges;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.HelperClasses;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
using Coordinate = (int x, int y);
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
@ -38,7 +42,7 @@ public class Day07 : IPuzzleSolver<long>
|
|||
|
||||
private Coordinate GetStart(string[] grid)
|
||||
{
|
||||
return new Coordinate(0, grid[0].IndexOf('S'));
|
||||
return (0, grid[0].IndexOf('S'));
|
||||
}
|
||||
|
||||
private long TraverseGridAndCountSplitters(string[] grid, Coordinate start)
|
||||
|
|
@ -49,18 +53,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[(int)current.X][(int)current.Y] == '^')
|
||||
if (grid[current.x][current.y] == '^')
|
||||
{
|
||||
splittersHit++;
|
||||
CheckAndEnqueueIfNotVisited(current with {Y = current.Y - 1});
|
||||
CheckAndEnqueueIfNotVisited(current with {Y = current.Y + 1});
|
||||
CheckAndEnqueueIfNotVisited((current.x, current.y - 1));
|
||||
CheckAndEnqueueIfNotVisited((current.x, current.y + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
CheckAndEnqueueIfNotVisited(current with {X = current.X + 1});
|
||||
CheckAndEnqueueIfNotVisited((current.x + 1, current.y));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -75,21 +79,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[(int)current.X][(int)current.Y] == '^')
|
||||
if (grid[current.x][current.y] == '^')
|
||||
{
|
||||
var resLeft = GetTimelinesCountRecursive(grid, current with {Y = current.Y - 1}, memory);
|
||||
var resRight = GetTimelinesCountRecursive(grid, current with {Y = current.Y + 1}, memory);
|
||||
var resLeft = GetTimelinesCountRecursive(grid, (current.x, current.y - 1), memory);
|
||||
var resRight = GetTimelinesCountRecursive(grid, (current.x, current.y + 1), memory);
|
||||
memory[current] = resLeft + resRight;
|
||||
return resLeft + resRight;
|
||||
}
|
||||
|
||||
var res = GetTimelinesCountRecursive(grid, current with {X = current.X + 1}, memory);
|
||||
var res = GetTimelinesCountRecursive(grid, (current.x + 1, current.y), memory);
|
||||
memory[current] = res;
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.Extensions;
|
||||
using AdventOfCode.HelperClasses;
|
||||
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);
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
@ -11,12 +15,12 @@ public class Day08 : IPuzzleSolver<long>
|
|||
{
|
||||
_amountToConnect = amountToConnect;
|
||||
}
|
||||
private Coordinate3d[] ParsePuzzleInput(string path)
|
||||
private Coordinate[] 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 => new Coordinate3d(numbers[0], numbers[1], numbers[2]))
|
||||
.Select(numbers => (numbers[0], numbers[1], numbers[2]))
|
||||
.ToArray();
|
||||
return puzzleInput;
|
||||
}
|
||||
|
|
@ -37,16 +41,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<Coordinate3d>> CreateCircuits(IEnumerable<Coordinate3d> jBoxes)
|
||||
private List<HashSet<Coordinate>> CreateCircuits(IEnumerable<Coordinate> jBoxes)
|
||||
{
|
||||
return jBoxes.Select(p => new HashSet<Coordinate3d> { p }).ToList();
|
||||
return jBoxes.Select(p => new HashSet<Coordinate> { p }).ToList();
|
||||
}
|
||||
|
||||
private List<HashSet<Coordinate3d>> CombineCircuits(List<HashSet<Coordinate3d>> circuits,
|
||||
IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> jBoxPairs, int amountToConnect)
|
||||
private List<HashSet<Coordinate>> CombineCircuits(List<HashSet<Coordinate>> circuits,
|
||||
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs, int amountToConnect)
|
||||
{
|
||||
circuits = jBoxPairs
|
||||
.Take(amountToConnect)
|
||||
|
|
@ -54,8 +58,8 @@ public class Day08 : IPuzzleSolver<long>
|
|||
return circuits;
|
||||
}
|
||||
|
||||
private (Coordinate3d a, Coordinate3d b) LastCombinedConnectAllCircuits(List<HashSet<Coordinate3d>> circuits,
|
||||
IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> jBoxPairs)
|
||||
private (Coordinate a, Coordinate b) LastCombinedConnectAllCircuits(List<HashSet<Coordinate>> circuits,
|
||||
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs)
|
||||
{
|
||||
foreach (var jBoxPair in jBoxPairs)
|
||||
{
|
||||
|
|
@ -63,10 +67,15 @@ public class Day08 : IPuzzleSolver<long>
|
|||
if(circuits.Count <= 1)
|
||||
return jBoxPair;
|
||||
}
|
||||
return (new(0,0,0), new(0,0,0));
|
||||
return ((0,0,0), (0,0,0));
|
||||
}
|
||||
|
||||
private List<HashSet<Coordinate3d>> ConnectJBoxes(Coordinate3d a, Coordinate3d b, List<HashSet<Coordinate3d>> circuits)
|
||||
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)
|
||||
{
|
||||
var circuitToAddBTo = circuits.First(circuit => circuit.Contains(a));
|
||||
var circuitToAddATo = circuits.First(circuit => circuit.Contains(b));
|
||||
|
|
@ -81,15 +90,25 @@ public class Day08 : IPuzzleSolver<long>
|
|||
return circuits;
|
||||
}
|
||||
|
||||
private IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate3d> jBoxes)
|
||||
private IEnumerable<(Coordinate boxA, Coordinate boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate> jBoxes)
|
||||
{
|
||||
var combinations = jBoxes.Combinations(2)
|
||||
.Select(x => (a: x[0], b: x[1]))
|
||||
.OrderBy(x => Coordinate3d.GetEuclidianDistance(x.a, x.b));
|
||||
var combinations = Combinations(jBoxes)
|
||||
.OrderBy(x => GetDistance(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<Coordinate3d>> circuits, int amountMultiply)
|
||||
private long MultiplyLargestCircuitLength(List<HashSet<Coordinate>> circuits, int amountMultiply)
|
||||
{
|
||||
return circuits
|
||||
.Select(circuit => (long)circuit.Count)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.Extensions;
|
||||
using AdventOfCode.HelperClasses;
|
||||
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 CoordinatePair = (long minX, long minY, long maxX, long maxY);
|
||||
|
||||
namespace AoC_2025;
|
||||
|
|
@ -12,7 +16,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 => new Coordinate(numbers[0], numbers[1]))
|
||||
.Select(numbers => (numbers[0], numbers[1]))
|
||||
.ToArray();
|
||||
return puzzleInput;
|
||||
}
|
||||
|
|
@ -38,7 +42,7 @@ public class Day09 : IPuzzleSolver<long>
|
|||
private IOrderedEnumerable<long> GetOrderedAreas(IEnumerable<CoordinatePair> containedRectangles)
|
||||
{
|
||||
return containedRectangles
|
||||
.Select(rectangle => GetArea(new Coordinate(rectangle.maxX, rectangle.maxY), new Coordinate(rectangle.minX, rectangle.minY)))
|
||||
.Select(rectangle => GetArea((rectangle.maxX, rectangle.maxY), (rectangle.minX, rectangle.minY)))
|
||||
.OrderDescending();
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +54,7 @@ public class Day09 : IPuzzleSolver<long>
|
|||
|
||||
private IEnumerable<CoordinatePair> GetAllPossibleRectanglesAsPairs(Coordinate[] redTiles)
|
||||
{
|
||||
return redTiles.Combinations(2).Select(x => AsPair(x[0], x[1]));
|
||||
return Combinations(redTiles).Select(x => AsPair(x.a, x.b));
|
||||
}
|
||||
|
||||
private CoordinatePair[] GetBordersAsPairs(Coordinate[] redTiles)
|
||||
|
|
@ -66,7 +70,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)
|
||||
|
|
@ -77,7 +81,18 @@ 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);
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,18 @@
|
|||
using System.Data;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
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)[] ParsePuzzleInput(string path)
|
||||
private (uint lamps, uint[] buttons, int[] joltages)[] ParsePuzzleInput(string path)
|
||||
{
|
||||
var puzzleInput = File.ReadAllLines(path)
|
||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||
|
|
@ -36,8 +40,16 @@ 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;
|
||||
}
|
||||
|
|
@ -202,7 +214,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 buttons.Combinations(i))
|
||||
foreach (var buttonPattern in Combinations(buttons, i))
|
||||
{
|
||||
var joltageLevels = GetJoltageLevelsForButtonPattern(patternLength, buttonPattern);
|
||||
yield return (joltageLevels, buttonPattern.Length);
|
||||
|
|
@ -259,4 +271,71 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,10 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,10 @@
|
|||
using AdvenOfCode.Contracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue