Refactor 2026 Day06 Part2 location calculation for better readability and efficiency
This commit is contained in:
1 parent
1e482b532c
commit
1faf120a47
2 files changed
+59
-62
No files matched your search
@@ -30,18 +30,18 @@ public class Day06Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Part02_Test_equals_35332647()
|
public void Part02_Test_equals_1222()
|
||||||
{
|
{
|
||||||
var actual = _sut.SolvePart2Test(TestInputPath);
|
var actual = _sut.SolvePart2Test(TestInputPath);
|
||||||
|
|
||||||
Assert.Equal("", actual);
|
Assert.Equal("12,22", actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Part02_Prod_equals_40767683()
|
public void Part02_Prod_equals_4336()
|
||||||
{
|
{
|
||||||
var actual = _sut.SolvePart2(ProdInputPath);
|
var actual = _sut.SolvePart2(ProdInputPath);
|
||||||
|
|
||||||
Assert.Equal("", actual);
|
Assert.Equal("43,36", actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+55
-58
@@ -14,14 +14,14 @@ public class Day06 : IPuzzleSolver<string>
|
|||||||
conn[1].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet()))
|
conn[1].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet()))
|
||||||
.ToDictionary();
|
.ToDictionary();
|
||||||
|
|
||||||
var missing = puzzleInput
|
AddMissingKeys(puzzleInput);
|
||||||
.SelectMany(kv => kv.Value.Where(v => !puzzleInput.ContainsKey(v)))
|
AddAllVertices(puzzleInput);
|
||||||
.ToArray();
|
|
||||||
foreach (var key in missing)
|
|
||||||
{
|
|
||||||
puzzleInput[key] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return puzzleInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddAllVertices(Dictionary<string, HashSet<string>> puzzleInput)
|
||||||
|
{
|
||||||
foreach (var (from, tos) in puzzleInput)
|
foreach (var (from, tos) in puzzleInput)
|
||||||
{
|
{
|
||||||
foreach (var to in tos)
|
foreach (var to in tos)
|
||||||
@@ -29,84 +29,81 @@ public class Day06 : IPuzzleSolver<string>
|
|||||||
puzzleInput[to].Add(from);
|
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)
|
public string SolvePart1(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var gridInstrucions = ParsePuzzleInput(pathToPuzzleInput);
|
var gridInstructions = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var possibleMiddles = gridInstrucions.Where(conn => conn.Value.Count == 4);
|
var possibleMiddles = gridInstructions.Where(conn => conn.Value.Count == 4);
|
||||||
var middle = possibleMiddles.MinBy(m => GetMaxDistanceFrom(m.Key, gridInstrucions));
|
var middle = possibleMiddles.MinBy(m => GetMaxDistanceFromEdge(m.Key, gridInstructions));
|
||||||
return middle.Key;
|
return middle.Key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string SolvePart2(string pathToPuzzleInput)
|
public string SolvePart2Test(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var gridInstrucions = ParsePuzzleInput(pathToPuzzleInput);
|
var gridInstructions = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
List<(string key, (int x, int y))> satellites = [("1", (47,30)), ("2", (29,59)), ("3", (56,35))];
|
List<(string key, (int x, int y))> satellites = [("1", (11,20)), ("2", (10,22)), ("3", (12,20))];
|
||||||
var location = DetermineLocation("2026", satellites, gridInstrucions);
|
var location = DetermineLocation("7", satellites, gridInstructions);
|
||||||
|
|
||||||
return $"{location.x},{location.y}";
|
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 intersections = GetPossibleLocations(key, satellites, gridInstructions);
|
||||||
|
|
||||||
|
while (intersections.Length > 1)
|
||||||
{
|
{
|
||||||
var nextSat = GetNextPossibleSatellite(satellites, gridInstrucions);
|
var nextSat = GetNextPossibleSatellite(satellites, gridInstructions);
|
||||||
satellites.Add(nextSat);
|
satellites.Add(nextSat);
|
||||||
intersections = GetPossibleLocations(key, satellites, gridInstrucions);
|
intersections = GetPossibleLocations(key, satellites, gridInstructions);
|
||||||
}
|
}
|
||||||
|
|
||||||
return intersections.First();
|
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
|
var nextSat = gridInstructions
|
||||||
.Where(x => satellites.All(sat => sat.key != x.Key))
|
.Where(satellite => satellites.All(sat => sat.key != satellite.Key))
|
||||||
.Select(x => (x.Key, GetPossibleLocations(x.Key, satellites, gridInstrucions)))
|
.Select(satellite => (satellite.Key, locations: GetPossibleLocations(satellite.Key, satellites, gridInstructions)))
|
||||||
.First(x => x.Item2.Count == 1);
|
.First(satellite => satellite.locations.Length == 1);
|
||||||
return (nextSat.Key, nextSat.Item2.First());
|
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 = [];
|
return
|
||||||
foreach (var (name, point) in satellites)
|
[
|
||||||
{
|
.. satellites
|
||||||
var distFromPoint = GetMinDistanceTo(name, key, gridInstrucions);
|
.Select(satellite => (satellite, dist: GetMinDistanceTo(satellite.key, key, gridInstructions)))
|
||||||
var pointsFromPoint = GetPointsOnRadius(point, distFromPoint);
|
.Select(tuple => GetPointsOnRadius(tuple.satellite.point, tuple.dist))
|
||||||
if(!intersections.Any())
|
.Aggregate((acc, next) => acc.Intersect(next))
|
||||||
intersections = [.. pointsFromPoint];
|
];
|
||||||
else
|
|
||||||
intersections = [..intersections.Intersect(pointsFromPoint)];
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
private static int GetMinDistanceTo(string from, string to, Dictionary<string, HashSet<string>> gridInstructions)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
var queue = new Queue<(string, int)>();
|
var queue = new Queue<(string, int)>();
|
||||||
queue.Enqueue((from, 0));
|
queue.Enqueue((from, 0));
|
||||||
@@ -116,7 +113,7 @@ public class Day06 : IPuzzleSolver<string>
|
|||||||
var (current, distance) = queue.Dequeue();
|
var (current, distance) = queue.Dequeue();
|
||||||
if (current == to) return distance;
|
if (current == to) return distance;
|
||||||
if(!seen.Add(current)) continue;
|
if(!seen.Add(current)) continue;
|
||||||
foreach (var next in gridInstrucions[current])
|
foreach (var next in gridInstructions[current])
|
||||||
{
|
{
|
||||||
queue.Enqueue((next, distance + 1));
|
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 maxDistance = 0;
|
||||||
var queue = new Queue<(string, int)>();
|
var queue = new Queue<(string, int)>();
|
||||||
|
|||||||
Reference in new issue
Block a user