Extract `Coordinate`, `IntegerRange` and `Coordinate3d` to new `AdventOfCode.HelperClasses` project and refactor solution for modularity and type consistency

This commit is contained in:
Sebastian Lindemeier 2025-12-12 13:14:17 +01:00
parent 85516cd783
commit aaf32260d0
13 changed files with 119 additions and 143 deletions

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,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

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

View File

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

View File

@ -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();

View File

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

View File

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

View File

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

View File

@ -1,11 +1,6 @@
using System; using AdvenOfCode.Contracts;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdventOfCode.Extensions; using AdventOfCode.Extensions;
using Coordinate = (long x, long y, long z); using AdventOfCode.HelperClasses;
namespace AoC_2025; namespace AoC_2025;
@ -16,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;
} }
@ -42,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)
@ -59,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)
{ {
@ -68,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));
@ -91,15 +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 = jBoxes.Combinations(2) var combinations = jBoxes.Combinations(2)
.Select(x => (a: x[0], b: x[1])) .Select(x => (a: x[0], b: x[1]))
.OrderBy(x => GetDistance(x.a, x.b)); .OrderBy(x => Coordinate3d.GetEuclidianDistance(x.a, x.b));
return combinations; return combinations;
} }
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)

View File

@ -1,11 +1,6 @@
using System; using AdvenOfCode.Contracts;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using AdventOfCode.Extensions; using AdventOfCode.Extensions;
using Coordinate = (long x, long y); using AdventOfCode.HelperClasses;
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;
@ -17,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;
} }
@ -43,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();
} }
@ -71,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)
@ -82,7 +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<(Coordinate a, Coordinate b)> GetBorders(Coordinate[] tiles) private IEnumerable<(Coordinate a, Coordinate b)> GetBorders(Coordinate[] tiles)

View File

@ -1,10 +1,4 @@
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.Extensions;
using AdventOfCode.HelperClasses; using AdventOfCode.HelperClasses;

View File

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

View File

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