158 lines
5.8 KiB
C#
158 lines
5.8 KiB
C#
using AdvenOfCode.Contracts;
|
|
using AdventOfCode.Extensions;
|
|
|
|
namespace AoC_2026;
|
|
|
|
public class Day06 : IPuzzleSolver<string>
|
|
{
|
|
private static Dictionary<string, HashSet<string>> ParsePuzzleInput(string path)
|
|
{
|
|
var puzzleInput = File.ReadAllLines(path)
|
|
.Where(l => !string.IsNullOrWhiteSpace(l))
|
|
.Select(l => l.Split('-', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
|
.Select(conn => (conn[0],
|
|
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] = [];
|
|
}
|
|
|
|
foreach (var (from, tos) in puzzleInput)
|
|
{
|
|
foreach (var to in tos)
|
|
{
|
|
puzzleInput[to].Add(from);
|
|
}
|
|
}
|
|
|
|
return puzzleInput;
|
|
}
|
|
|
|
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));
|
|
return middle.Key;
|
|
}
|
|
|
|
public string SolvePart2(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);
|
|
|
|
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)
|
|
{
|
|
var intersections = GetPossibleLocations(key, satellites, gridInstrucions);
|
|
|
|
while (intersections.Count > 1)
|
|
{
|
|
var nextSat = GetNextPossibleSatellite(satellites, gridInstrucions);
|
|
satellites.Add(nextSat);
|
|
intersections = GetPossibleLocations(key, satellites, gridInstrucions);
|
|
}
|
|
|
|
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)
|
|
{
|
|
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());
|
|
}
|
|
|
|
private static List<(int x, int y)> GetPossibleLocations(string key, List<(string key, (int x, int y))> satellites, Dictionary<string, HashSet<string>> gridInstrucions)
|
|
{
|
|
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 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)
|
|
{
|
|
var queue = new Queue<(string, int)>();
|
|
queue.Enqueue((from, 0));
|
|
var seen = new HashSet<string>();
|
|
while (queue.Count > 0)
|
|
{
|
|
var (current, distance) = queue.Dequeue();
|
|
if (current == to) return distance;
|
|
if(!seen.Add(current)) continue;
|
|
foreach (var next in gridInstrucions[current])
|
|
{
|
|
queue.Enqueue((next, distance + 1));
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
private static IEnumerable<(int x, int y)> GetPointsOnRadius((int x, int y) center, int radius)
|
|
{
|
|
var (x1, y1) = center;
|
|
for(var x2 = -radius; x2 <= radius; x2++)
|
|
{
|
|
var y2 = Math.Abs(Math.Abs(x2) - radius);
|
|
yield return (x1 + x2, y1 + y2);
|
|
if(y2 != 0)
|
|
yield return (x1 + x2, y1 - y2);
|
|
}
|
|
}
|
|
|
|
private static int GetMaxDistanceFrom(string from, Dictionary<string, HashSet<string>> gridInstrucions)
|
|
{
|
|
var maxDistance = 0;
|
|
var queue = new Queue<(string, int)>();
|
|
queue.Enqueue((from, 0));
|
|
var seen = new HashSet<string>();
|
|
while (queue.Count > 0)
|
|
{
|
|
var (current, distance) = queue.Dequeue();
|
|
if(!seen.Add(current))
|
|
continue;
|
|
maxDistance = Math.Max(maxDistance, distance);
|
|
foreach (var next in gridInstrucions[current])
|
|
{
|
|
queue.Enqueue((next, distance + 1));
|
|
}
|
|
}
|
|
return maxDistance;
|
|
}
|
|
} |