Compare commits
3 Commits
d92293f631
...
aaf32260d0
| Author | SHA1 | Date |
|---|---|---|
|
|
aaf32260d0 | |
|
|
85516cd783 | |
|
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,10 @@ 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
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.HelperClasses", "AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj", "{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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
|
||||||
|
{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
|
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,8 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj" />
|
<ProjectReference Include="..\AdvenOfCode.Contracts\AdvenOfCode.Contracts.csproj" />
|
||||||
|
<ProjectReference Include="..\AdventOfCode.Extensions\AdventOfCode.Extensions.csproj" />
|
||||||
|
<ProjectReference Include="..\AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
using System;
|
using AdvenOfCode.Contracts;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
using System;
|
using AdvenOfCode.Contracts;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using AdvenOfCode.Contracts;
|
||||||
using System.IO;
|
using AdventOfCode.HelperClasses;
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
using Coordinate = (int x, int y);
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
@ -64,7 +61,7 @@ public class Day04 : IPuzzleSolver<long>
|
||||||
{
|
{
|
||||||
var indexedCharacters = gridLine.Index();
|
var indexedCharacters = gridLine.Index();
|
||||||
var validRolls = indexedCharacters.Where(tuple => tuple.Item == '@');
|
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;
|
return rollCoordinates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,12 +76,12 @@ public class Day04 : IPuzzleSolver<long>
|
||||||
{
|
{
|
||||||
Coordinate[] neighbourDirections =
|
Coordinate[] neighbourDirections =
|
||||||
[
|
[
|
||||||
(-1, -1), (-1, 0), (-1, 1),
|
new(-1, -1), new(-1, 0), new(-1, 1),
|
||||||
(0, -1), (0, 1),
|
new(0, -1), new(0, 1),
|
||||||
(1, -1), (1, 0), (1, 1),
|
new(1, -1), new(1, 0), new(1, 1),
|
||||||
];
|
];
|
||||||
var possibleNeighbours = neighbourDirections
|
var possibleNeighbours = neighbourDirections
|
||||||
.Select(direction => (coordinate.x + direction.x, coordinate.y + direction.y));
|
.Select(direction => coordinate + direction);
|
||||||
var actualNeighbours = possibleNeighbours
|
var actualNeighbours = possibleNeighbours
|
||||||
.Where(coord => coordinates.Contains(coord))
|
.Where(coord => coordinates.Contains(coord))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using AdvenOfCode.Contracts;
|
||||||
using System.IO;
|
using AdventOfCode.HelperClasses;
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
using Range = (long from, long to);
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
@ -52,54 +49,49 @@ public class Day05 : IPuzzleSolver<long>
|
||||||
return parsedIds;
|
return parsedIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Range[] ParseRanges(IEnumerable<string> ranges)
|
private IntegerRange[] ParseRanges(IEnumerable<string> ranges)
|
||||||
{
|
{
|
||||||
return ranges.Select(range =>
|
return ranges.Select(range =>
|
||||||
{
|
{
|
||||||
var split = range.Split('-', StringSplitOptions.RemoveEmptyEntries);
|
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)
|
.OrderBy(range => range.start)
|
||||||
.ThenBy(range => range.Item2)
|
.ThenBy(range => range.end)
|
||||||
.ToArray();
|
.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));
|
var freshIds = ids.Where(id => IsIdInRanges(id, freshIdRanges));
|
||||||
return freshIds;
|
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 sumFreshIds = 0L;
|
||||||
var highestSeenId = -1L;
|
var highestSeenId = -1L;
|
||||||
foreach (var range in ranges)
|
foreach (var range in ranges)
|
||||||
{
|
{
|
||||||
var lowestNewId = Math.Max(highestSeenId + 1, range.from);
|
var lowestNewId = Math.Max(highestSeenId + 1, range.start);
|
||||||
highestSeenId = Math.Max(highestSeenId, range.to);
|
highestSeenId = Math.Max(highestSeenId, range.end);
|
||||||
sumFreshIds += Math.Max(0, highestSeenId - lowestNewId + 1);
|
sumFreshIds += Math.Max(0, highestSeenId - lowestNewId + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sumFreshIds;
|
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)
|
foreach (var range in ranges)
|
||||||
{
|
{
|
||||||
if (TryCombineRanges(optimizedRanges[^1], range, out var combined))
|
if (optimizedRanges[^1].TryCombine(range, out var combined))
|
||||||
{
|
{
|
||||||
optimizedRanges[^1] = combined;
|
optimizedRanges[^1] = combined;
|
||||||
}
|
}
|
||||||
|
|
@ -111,29 +103,10 @@ public class Day05 : IPuzzleSolver<long>
|
||||||
|
|
||||||
return optimizedRanges;
|
return optimizedRanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryCombineRanges(Range first, Range second, out Range combined)
|
|
||||||
{
|
|
||||||
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)
|
private static IEnumerable<long> CountIdsInRanges(IEnumerable<IntegerRange> combinedRanges)
|
||||||
{
|
{
|
||||||
var countIdsInRanges = combinedRanges.Select(range => range.to - range.from + 1);
|
var countIdsInRanges = combinedRanges.Select(range => range.Count());
|
||||||
return countIdsInRanges;
|
return countIdsInRanges;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using AdvenOfCode.Contracts;
|
||||||
using System.Data.Common;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using AdvenOfCode.Contracts;
|
||||||
using System.Data.Common;
|
using AdventOfCode.HelperClasses;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
using Coordinate = (int x, int y);
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
@ -42,7 +38,7 @@ public class Day07 : IPuzzleSolver<long>
|
||||||
|
|
||||||
private Coordinate GetStart(string[] grid)
|
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)
|
private long TraverseGridAndCountSplitters(string[] grid, Coordinate start)
|
||||||
|
|
@ -53,18 +49,18 @@ public class Day07 : IPuzzleSolver<long>
|
||||||
var splittersHit = 0L;
|
var splittersHit = 0L;
|
||||||
while (seen.TryDequeue(out var current))
|
while (seen.TryDequeue(out var current))
|
||||||
{
|
{
|
||||||
if (current.x >= grid.Length) continue;
|
if (current.X >= grid.Length) continue;
|
||||||
if (current.y >= grid[0].Length || current.y < 0) 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++;
|
splittersHit++;
|
||||||
CheckAndEnqueueIfNotVisited((current.x, current.y - 1));
|
CheckAndEnqueueIfNotVisited(current with {Y = current.Y - 1});
|
||||||
CheckAndEnqueueIfNotVisited((current.x, current.y + 1));
|
CheckAndEnqueueIfNotVisited(current with {Y = current.Y + 1});
|
||||||
}
|
}
|
||||||
else
|
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
|
// Dictionary only for memoization
|
||||||
private long GetTimelinesCountRecursive(string[] grid, Coordinate current, Dictionary<Coordinate, long> memory)
|
private long GetTimelinesCountRecursive(string[] grid, Coordinate current, Dictionary<Coordinate, long> memory)
|
||||||
{
|
{
|
||||||
if (current.x >= grid.Length)
|
if (current.X >= grid.Length)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(memory.TryGetValue(current, out var count)) return count;
|
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 resLeft = GetTimelinesCountRecursive(grid, current with {Y = current.Y - 1}, memory);
|
||||||
var resRight = GetTimelinesCountRecursive(grid, (current.x, current.y + 1), memory);
|
var resRight = GetTimelinesCountRecursive(grid, current with {Y = current.Y + 1}, memory);
|
||||||
memory[current] = resLeft + resRight;
|
memory[current] = resLeft + resRight;
|
||||||
return 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;
|
memory[current] = res;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,6 @@
|
||||||
using System;
|
using AdvenOfCode.Contracts;
|
||||||
using System.Collections.Generic;
|
using AdventOfCode.Extensions;
|
||||||
using System.Data.Common;
|
using AdventOfCode.HelperClasses;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
using Coordinate = (long x, long y, long z);
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
@ -15,12 +11,12 @@ public class Day08 : IPuzzleSolver<long>
|
||||||
{
|
{
|
||||||
_amountToConnect = amountToConnect;
|
_amountToConnect = amountToConnect;
|
||||||
}
|
}
|
||||||
private Coordinate[] ParsePuzzleInput(string path)
|
private Coordinate3d[] ParsePuzzleInput(string path)
|
||||||
{
|
{
|
||||||
var puzzleInput = File.ReadAllLines(path)
|
var puzzleInput = File.ReadAllLines(path)
|
||||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||||
.Select(line => line.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(str => long.Parse(str)).ToArray())
|
.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();
|
.ToArray();
|
||||||
return puzzleInput;
|
return puzzleInput;
|
||||||
}
|
}
|
||||||
|
|
@ -41,16 +37,16 @@ public class Day08 : IPuzzleSolver<long>
|
||||||
var jBoxPairs = GetAllCombinationsSortedByDistance(jBoxes);
|
var jBoxPairs = GetAllCombinationsSortedByDistance(jBoxes);
|
||||||
var circuits = CreateCircuits(jBoxes);
|
var circuits = CreateCircuits(jBoxes);
|
||||||
var lastConnected = LastCombinedConnectAllCircuits(circuits, jBoxPairs);
|
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,
|
private List<HashSet<Coordinate3d>> CombineCircuits(List<HashSet<Coordinate3d>> circuits,
|
||||||
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs, int amountToConnect)
|
IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> jBoxPairs, int amountToConnect)
|
||||||
{
|
{
|
||||||
circuits = jBoxPairs
|
circuits = jBoxPairs
|
||||||
.Take(amountToConnect)
|
.Take(amountToConnect)
|
||||||
|
|
@ -58,8 +54,8 @@ public class Day08 : IPuzzleSolver<long>
|
||||||
return circuits;
|
return circuits;
|
||||||
}
|
}
|
||||||
|
|
||||||
private (Coordinate a, Coordinate b) LastCombinedConnectAllCircuits(List<HashSet<Coordinate>> circuits,
|
private (Coordinate3d a, Coordinate3d b) LastCombinedConnectAllCircuits(List<HashSet<Coordinate3d>> circuits,
|
||||||
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs)
|
IEnumerable<(Coordinate3d boxA, Coordinate3d boxB)> jBoxPairs)
|
||||||
{
|
{
|
||||||
foreach (var jBoxPair in jBoxPairs)
|
foreach (var jBoxPair in jBoxPairs)
|
||||||
{
|
{
|
||||||
|
|
@ -67,15 +63,10 @@ public class Day08 : IPuzzleSolver<long>
|
||||||
if(circuits.Count <= 1)
|
if(circuits.Count <= 1)
|
||||||
return jBoxPair;
|
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)
|
private List<HashSet<Coordinate3d>> ConnectJBoxes(Coordinate3d a, Coordinate3d b, List<HashSet<Coordinate3d>> circuits)
|
||||||
{
|
|
||||||
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 circuitToAddBTo = circuits.First(circuit => circuit.Contains(a));
|
||||||
var circuitToAddATo = circuits.First(circuit => circuit.Contains(b));
|
var circuitToAddATo = circuits.First(circuit => circuit.Contains(b));
|
||||||
|
|
@ -90,25 +81,15 @@ public class Day08 : IPuzzleSolver<long>
|
||||||
return circuits;
|
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)
|
var combinations = jBoxes.Combinations(2)
|
||||||
.OrderBy(x => GetDistance(x.a, x.b));
|
.Select(x => (a: x[0], b: x[1]))
|
||||||
|
.OrderBy(x => Coordinate3d.GetEuclidianDistance(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<Coordinate3d>> circuits, int amountMultiply)
|
||||||
{
|
{
|
||||||
return circuits
|
return circuits
|
||||||
.Select(circuit => (long)circuit.Count)
|
.Select(circuit => (long)circuit.Count)
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,6 @@
|
||||||
using System;
|
using AdvenOfCode.Contracts;
|
||||||
using System.Collections.Generic;
|
using AdventOfCode.Extensions;
|
||||||
using System.Data.Common;
|
using AdventOfCode.HelperClasses;
|
||||||
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);
|
using CoordinatePair = (long minX, long minY, long maxX, long maxY);
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
@ -16,7 +12,7 @@ public class Day09 : IPuzzleSolver<long>
|
||||||
var puzzleInput = File.ReadAllLines(path)
|
var puzzleInput = File.ReadAllLines(path)
|
||||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||||
.Select(line => line.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(str => long.Parse(str)).ToArray())
|
.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();
|
.ToArray();
|
||||||
return puzzleInput;
|
return puzzleInput;
|
||||||
}
|
}
|
||||||
|
|
@ -42,7 +38,7 @@ public class Day09 : IPuzzleSolver<long>
|
||||||
private IOrderedEnumerable<long> GetOrderedAreas(IEnumerable<CoordinatePair> containedRectangles)
|
private IOrderedEnumerable<long> GetOrderedAreas(IEnumerable<CoordinatePair> containedRectangles)
|
||||||
{
|
{
|
||||||
return 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();
|
.OrderDescending();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,7 +50,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)
|
||||||
|
|
@ -70,7 +66,7 @@ public class Day09 : IPuzzleSolver<long>
|
||||||
|
|
||||||
private CoordinatePair AsPair(Coordinate a, Coordinate b)
|
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)
|
private bool RectangleCrossesBorder(CoordinatePair rectangle, CoordinatePair borderSegment)
|
||||||
|
|
@ -81,18 +77,7 @@ public class Day09 : IPuzzleSolver<long>
|
||||||
|
|
||||||
private long GetArea(Coordinate tileA, Coordinate tileB)
|
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)
|
private IEnumerable<(Coordinate a, Coordinate b)> GetBorders(Coordinate[] tiles)
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,14 @@
|
||||||
using System;
|
using System.Data;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
using AdvenOfCode.Contracts;
|
||||||
|
using AdventOfCode.Extensions;
|
||||||
|
using AdventOfCode.HelperClasses;
|
||||||
using Microsoft.Z3;
|
using Microsoft.Z3;
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
public class Day10 : IPuzzleSolver<long>
|
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)
|
var puzzleInput = File.ReadAllLines(path)
|
||||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
.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)))
|
.Select(lampIndexes => lampIndexes.Aggregate((uint)0L, (acc, lampIndex) => acc | ((uint)1 << lampIndex)))
|
||||||
.ToArray())
|
.ToArray())
|
||||||
.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
|
var res = lamps
|
||||||
.Zip(buttons, (lamp, button) => (lamp, button))
|
.Zip(buttons, (lamp, button) => (lamp, button))
|
||||||
.Zip(joltages, (bl, joltage) => (bl.lamp, bl.button, joltage))
|
|
||||||
.ToArray();
|
.ToArray();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
@ -214,7 +202,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,71 +259,4 @@ 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 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,10 +1,4 @@
|
||||||
using System;
|
using AdvenOfCode.Contracts;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,4 @@
|
||||||
using System;
|
using AdvenOfCode.Contracts;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using AdvenOfCode.Contracts;
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue