Files
Advent-Of-Code/AoC_2026/Day06.cs
T

160 lines
5.6 KiB
C#

using System.Net.Http.Headers;
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))
.SelectMany(conn => conn[1]
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(to => (from: conn[0], to))
);
var connectionsGrid = GetConnectionsGrid(puzzleInput);
return connectionsGrid;
}
private static Dictionary<string, HashSet<string>> GetConnectionsGrid(IEnumerable<(string, string)> puzzleInput)
{
var grid = new Dictionary<string, HashSet<string>>();
foreach (var (from, to) in puzzleInput)
{
AddOrUpdate(grid, from, [to]);
AddOrUpdate(grid, to, [from]);
}
return grid;
}
private static void AddOrUpdate<TKey, TValue>(Dictionary<TKey, HashSet<TValue>> dictionary, TKey key,
HashSet<TValue> value) where TKey : notnull
{
if(dictionary.TryGetValue(key, out var existing))
existing.UnionWith(value);
else
dictionary[key] = value;
}
public string SolvePart1(string pathToPuzzleInput)
{
var grid = ParsePuzzleInput(pathToPuzzleInput);
var middle = GetMiddleNode(grid);
return middle;
}
public string SolvePart2Test(string pathToPuzzleInput)
{
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}";
}
public string SolvePart2(string pathToPuzzleInput)
{
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);
return $"{location.x},{location.y}";
}
private static string GetMiddleNode(Dictionary<string, HashSet<string>> grid)
{
while (grid.Keys.Count > 1)
{
var edges = GetEdges(grid);
foreach (var edge in edges)
{
grid.Remove(edge);
}
foreach (var (_, connections) in grid)
{
connections.ExceptWith(edges);
}
}
return grid.First().Key;
}
private static string[] GetEdges(Dictionary<string, HashSet<string>> grid)
{
var edges = grid
.Where(conn => conn.Value.Count < 4)
.Select(conn => conn.Key)
.ToArray();
return edges;
}
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, gridInstructions);
satellites.Add(nextSat);
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) point)> satellites, Dictionary<string, HashSet<string>> gridInstructions)
{
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 (int x, int y)[] GetPossibleLocations(string key, List<(string key, (int x, int y) point)> satellites, Dictionary<string, HashSet<string>> gridInstructions)
{
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))
];
}
private static int GetMinDistanceTo(string from, string to, Dictionary<string, HashSet<string>> gridInstructions)
{
var queue = new Queue<(string, int)>();
var seen = new HashSet<string>();
queue.Enqueue((from, 0));
while (queue.Count > 0)
{
var (current, distance) = queue.Dequeue();
if (current == to) return distance;
if(!seen.Add(current)) continue;
foreach (var next in gridInstructions[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);
yield return (x1 + x2, y1 - y2);
}
}
}