Refactor 2026 Day06 Part2 location calculation for better readability and efficiency

This commit is contained in:
Sebastian Lindemeier committed 2026-12-06 13:37:31 +01:00
1 parent 1e482b532c
commit 1faf120a47
2 files changed
+58 -61

No files matched your search

+4 -4
View File
@@ -30,18 +30,18 @@ public class Day06Test
}
[Fact]
public void Part02_Test_equals_35332647()
public void Part02_Test_equals_1222()
{
var actual = _sut.SolvePart2Test(TestInputPath);
Assert.Equal("", actual);
Assert.Equal("12,22", actual);
}
[Fact]
public void Part02_Prod_equals_40767683()
public void Part02_Prod_equals_4336()
{
var actual = _sut.SolvePart2(ProdInputPath);
Assert.Equal("", actual);
Assert.Equal("43,36", actual);
}
}
+54 -57
View File
@@ -14,14 +14,14 @@ public class Day06 : IPuzzleSolver<string>
conn[1].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet()))
.ToDictionary();
var missing = puzzleInput
.SelectMany(kv => kv.Value.Where(v => !puzzleInput.ContainsKey(v)))
.ToArray();
foreach (var key in missing)
{
puzzleInput[key] = [];
AddMissingKeys(puzzleInput);
AddAllVertices(puzzleInput);
return puzzleInput;
}
private static void AddAllVertices(Dictionary<string, HashSet<string>> puzzleInput)
{
foreach (var (from, tos) in puzzleInput)
{
foreach (var to in tos)
@@ -29,84 +29,81 @@ public class Day06 : IPuzzleSolver<string>
puzzleInput[to].Add(from);
}
}
}
return puzzleInput;
private static void AddMissingKeys(Dictionary<string, HashSet<string>> puzzleInput)
{
var missing = puzzleInput
.SelectMany(kv => kv.Value.Where(v => !puzzleInput.ContainsKey(v)))
.ToArray();
foreach (var key in missing)
{
puzzleInput[key] = [];
}
}
public string SolvePart1(string pathToPuzzleInput)
{
var gridInstrucions = ParsePuzzleInput(pathToPuzzleInput);
var possibleMiddles = gridInstrucions.Where(conn => conn.Value.Count == 4);
var middle = possibleMiddles.MinBy(m => GetMaxDistanceFrom(m.Key, gridInstrucions));
var gridInstructions = ParsePuzzleInput(pathToPuzzleInput);
var possibleMiddles = gridInstructions.Where(conn => conn.Value.Count == 4);
var middle = possibleMiddles.MinBy(m => GetMaxDistanceFromEdge(m.Key, gridInstructions));
return middle.Key;
}
public string SolvePart2(string pathToPuzzleInput)
public string SolvePart2Test(string pathToPuzzleInput)
{
var gridInstrucions = ParsePuzzleInput(pathToPuzzleInput);
List<(string key, (int x, int y))> satellites = [("1", (47,30)), ("2", (29,59)), ("3", (56,35))];
var location = DetermineLocation("2026", satellites, gridInstrucions);
var gridInstructions = ParsePuzzleInput(pathToPuzzleInput);
List<(string key, (int x, int y))> satellites = [("1", (11,20)), ("2", (10,22)), ("3", (12,20))];
var location = DetermineLocation("7", satellites, gridInstructions);
return $"{location.x},{location.y}";
}
private static (int x, int y) DetermineLocation(string key, List<(string key, (int x, int y))> satellites, Dictionary<string, HashSet<string>> gridInstrucions)
public string SolvePart2(string pathToPuzzleInput)
{
var intersections = GetPossibleLocations(key, satellites, gridInstrucions);
var gridInstructions = ParsePuzzleInput(pathToPuzzleInput);
List<(string key, (int x, int y))> satellites = [("1", (47,30)), ("2", (29,59)), ("3", (56,35))];
var location = DetermineLocation("2026", satellites, gridInstructions);
while (intersections.Count > 1)
return $"{location.x},{location.y}";
}
private static (int x, int y) DetermineLocation(string key, List<(string key, (int x, int y))> satellites, Dictionary<string, HashSet<string>> gridInstructions)
{
var nextSat = GetNextPossibleSatellite(satellites, gridInstrucions);
var intersections = GetPossibleLocations(key, satellites, gridInstructions);
while (intersections.Length > 1)
{
var nextSat = GetNextPossibleSatellite(satellites, gridInstructions);
satellites.Add(nextSat);
intersections = GetPossibleLocations(key, satellites, gridInstrucions);
intersections = GetPossibleLocations(key, satellites, gridInstructions);
}
return intersections.First();
}
private static (string Key, (int x, int y) Pos) GetNextPossibleSatellite(List<(string key, (int x, int y))> satellites, Dictionary<string, HashSet<string>> gridInstrucions)
private static (string Key, (int x, int y) Pos) GetNextPossibleSatellite(List<(string key, (int x, int y) point)> satellites, Dictionary<string, HashSet<string>> gridInstructions)
{
var nextSat = gridInstrucions
.Where(x => satellites.All(sat => sat.key != x.Key))
.Select(x => (x.Key, GetPossibleLocations(x.Key, satellites, gridInstrucions)))
.First(x => x.Item2.Count == 1);
return (nextSat.Key, nextSat.Item2.First());
var nextSat = gridInstructions
.Where(satellite => satellites.All(sat => sat.key != satellite.Key))
.Select(satellite => (satellite.Key, locations: GetPossibleLocations(satellite.Key, satellites, gridInstructions)))
.First(satellite => satellite.locations.Length == 1);
return (nextSat.Key, nextSat.locations.First());
}
private static List<(int x, int y)> GetPossibleLocations(string key, List<(string key, (int x, int y))> satellites, Dictionary<string, HashSet<string>> gridInstrucions)
private static (int x, int y)[] GetPossibleLocations(string key, List<(string key, (int x, int y) point)> satellites, Dictionary<string, HashSet<string>> gridInstructions)
{
List<(int x, int y)> intersections = [];
foreach (var (name, point) in satellites)
{
var distFromPoint = GetMinDistanceTo(name, key, gridInstrucions);
var pointsFromPoint = GetPointsOnRadius(point, distFromPoint);
if(!intersections.Any())
intersections = [.. pointsFromPoint];
else
intersections = [..intersections.Intersect(pointsFromPoint)];
return
[
.. satellites
.Select(satellite => (satellite, dist: GetMinDistanceTo(satellite.key, key, gridInstructions)))
.Select(tuple => GetPointsOnRadius(tuple.satellite.point, tuple.dist))
.Aggregate((acc, next) => acc.Intersect(next))
];
}
return intersections;
}
public string SolvePart2Test(string pathToPuzzleInput)
{
var gridInstrucions = ParsePuzzleInput(pathToPuzzleInput);
var distFromOne = GetMinDistanceTo("1", "7", gridInstrucions);
var distFromTwo = GetMinDistanceTo("2", "7", gridInstrucions);
var distFromThree = GetMinDistanceTo("3", "7", gridInstrucions);
var pointsFromOne = GetPointsOnRadius((11,20), distFromOne);
var pointsFromTwo = GetPointsOnRadius((10,22), distFromTwo);
var pointsFromThree = GetPointsOnRadius((12,20), distFromThree);
var intersections = pointsFromOne.Intersect(pointsFromTwo).Intersect(pointsFromThree);
var intersection = intersections.First();
return $"{intersection.x},{intersection.y}";
}
private static int GetMinDistanceTo(string from, string to, Dictionary<string, HashSet<string>> gridInstrucions)
private static int GetMinDistanceTo(string from, string to, Dictionary<string, HashSet<string>> gridInstructions)
{
var queue = new Queue<(string, int)>();
queue.Enqueue((from, 0));
@@ -116,7 +113,7 @@ public class Day06 : IPuzzleSolver<string>
var (current, distance) = queue.Dequeue();
if (current == to) return distance;
if(!seen.Add(current)) continue;
foreach (var next in gridInstrucions[current])
foreach (var next in gridInstructions[current])
{
queue.Enqueue((next, distance + 1));
}
@@ -136,7 +133,7 @@ public class Day06 : IPuzzleSolver<string>
}
}
private static int GetMaxDistanceFrom(string from, Dictionary<string, HashSet<string>> gridInstrucions)
private static int GetMaxDistanceFromEdge(string from, Dictionary<string, HashSet<string>> gridInstrucions)
{
var maxDistance = 0;
var queue = new Queue<(string, int)>();