Add 2026 Day06 solution and tests
This commit is contained in:
1 parent
9bc55fb4ed
commit
1e482b532c
3 files changed
+206
-1
No files matched your search
@@ -0,0 +1,47 @@
|
||||
using AdvenOfCode.Contracts;
|
||||
|
||||
namespace AoC_2026.Tests;
|
||||
|
||||
public class Day06Test
|
||||
{
|
||||
private readonly Day06 _sut;
|
||||
private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||||
private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day06.txt";
|
||||
private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day06.txt";
|
||||
|
||||
public Day06Test()
|
||||
{
|
||||
_sut = new Day06();
|
||||
}
|
||||
[Fact]
|
||||
public void Part01_Test_equals_6()
|
||||
{
|
||||
var actual = _sut.SolvePart1(TestInputPath);
|
||||
|
||||
Assert.Equal("6", actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Part01_Prod_equals_827()
|
||||
{
|
||||
var actual = _sut.SolvePart1(ProdInputPath);
|
||||
|
||||
Assert.Equal("827", actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Part02_Test_equals_35332647()
|
||||
{
|
||||
var actual = _sut.SolvePart2Test(TestInputPath);
|
||||
|
||||
Assert.Equal("", actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Part02_Prod_equals_40767683()
|
||||
{
|
||||
var actual = _sut.SolvePart2(ProdInputPath);
|
||||
|
||||
Assert.Equal("", actual);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ namespace AoC_2026;
|
||||
|
||||
public class Day01 : IPuzzleSolver<long>
|
||||
{
|
||||
private int[] ParsePuzzleInput(string path)
|
||||
private static int[] ParsePuzzleInput(string path)
|
||||
{
|
||||
var puzzleInput = File.ReadAllLines(path)
|
||||
.Where(l => !string.IsNullOrWhiteSpace(l))
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user