diff --git a/AdvendOfCode.Runner/AdventOfCode.Runner.csproj b/AdvendOfCode.Runner/AdventOfCode.Runner.csproj index 39f8435..eab4bb4 100644 --- a/AdvendOfCode.Runner/AdventOfCode.Runner.csproj +++ b/AdvendOfCode.Runner/AdventOfCode.Runner.csproj @@ -8,6 +8,11 @@ enable + + + + + diff --git a/AdvendOfCode.Runner/Program.cs b/AdvendOfCode.Runner/Program.cs index 16d13ee..c2d34ce 100644 --- a/AdvendOfCode.Runner/Program.cs +++ b/AdvendOfCode.Runner/Program.cs @@ -1,14 +1,27 @@ -using AdventOfCode.Solutions._2023; +using AdventOfCode.Solutions; using AdventOfCode.Core.Shared; using AdventOfCode.Core; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; -InputReader inputReader = new() -{ - //IsDebug = true -}; -IChallange challange = new Day12(inputReader); +IChallange challange = Host.CreateDefaultBuilder() + .ConfigureServices(ConfigureServices) + .Build() + .Services + .GetService() + .GetChallange(2024, 1); Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}"); -Console.WriteLine($"Part 2: {await challange.GetSolutionPart2()}"); \ No newline at end of file +Console.WriteLine($"Part 2: {await challange.GetSolutionPart2()}"); + +static void ConfigureServices(IServiceCollection services) +{ + + + services + .AddChallanges() + .AddTransient() + .AddScoped(); +} \ No newline at end of file diff --git a/AdventOfCode.Core/Shared/A-Star/AStarGrid.cs b/AdventOfCode.Core/Shared/A-Star/AStarGrid.cs index c6e5a7a..632d1e8 100644 --- a/AdventOfCode.Core/Shared/A-Star/AStarGrid.cs +++ b/AdventOfCode.Core/Shared/A-Star/AStarGrid.cs @@ -1,20 +1,19 @@ namespace AdventOfCode.Core.Shared.A_Star { - public class AStarGrid(AStarNode[] data) : Grid(data) + public class AStarGrid(T[] data) : Grid(data) where T : AStarNode { public int TileMoveCost { get; set; } - public int MinimalSteps(AStarNode start, AStarNode destination) + public int MinimalSteps(T start, T destination) { - List openNodes = [start]; - List closedNodes = []; + List openNodes = [start]; + List closedNodes = []; while(openNodes.Count != 0) { // get the lowest F-cost node - AStarNode current = openNodes.OrderBy(n => n.FCost).First(); + T current = openNodes.OrderBy(n => n.FCost).First(); openNodes.Remove(current); - current.CloseNode(); closedNodes.Add(current); if (current == destination) @@ -23,22 +22,33 @@ return 1; } - var neighbors = GetNeighbors(current, false); - neighbors = neighbors.Where(n => current.CanMoveTo(current)); + var neighbors = GetNeighbors(current, false) + .Where(n => current.CanMoveTo(current)); // calc costs - foreach(AStarNode neighbor in neighbors) + foreach(T neighbor in neighbors) { - neighbor.ParentNode = current; - // since we only move straight the move cost is always the same - neighbor.GCost = current.GCost + TileMoveCost; - neighbor.HCost = neighbor.GetManhattanDistance(destination) * TileMoveCost; - neighbor.OpenNode(); + long g = current.GCost + GetMoveCost(current, neighbor); + long h = neighbor.GetManhattanDistance(destination) * GetMoveCost(current, neighbor); + long f = g + h; + + int openNodeIndex = openNodes.IndexOf(neighbor), + closedNodeIndex = closedNodes.IndexOf(neighbor); + + if (openNodeIndex > 0 && openNodes[openNodeIndex].FCost > f) + { + openNodes[openNodeIndex].ParentNode = neighbor; + openNodes[openNodeIndex].GCost = g; + openNodes[openNodeIndex].HCost = h; + } + openNodes.Add(neighbor); } } return -1; } + + public virtual int GetMoveCost(T current, T neighbor) => TileMoveCost; } } diff --git a/AdventOfCode.Core/Shared/A-Star/AStarNode.cs b/AdventOfCode.Core/Shared/A-Star/AStarNode.cs index a72dd7d..15d7130 100644 --- a/AdventOfCode.Core/Shared/A-Star/AStarNode.cs +++ b/AdventOfCode.Core/Shared/A-Star/AStarNode.cs @@ -19,18 +19,14 @@ namespace AdventOfCode.Core.Shared.A_Star /// public long FCost => GCost + HCost; - public bool? IsClosed { get; private set; } = null; - public AStarNode? ParentNode { get; set; } + public AStarNode() { } + public AStarNode(Point position) : base(position) { } public AStarNode(int x, int y, char value) : base(x, y, value) { } - public void OpenNode() => IsClosed = false; - - public void CloseNode() => IsClosed = true; - public virtual bool CanMoveTo(AStarNode target) => true; } } diff --git a/AdventOfCode.Solutions/2023/Day 00/Day00.cs b/AdventOfCode.Solutions/2023/Day 00/Day00.cs deleted file mode 100644 index fe9a851..0000000 --- a/AdventOfCode.Solutions/2023/Day 00/Day00.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace AdventOfCode.Solutions._2023 -{ - public class Day00 : IChallange - { - public int Year => 0; - - public int Day => 0; - - private readonly InputReader _inputReader; - - public Day00(InputReader inputReader) - { - _inputReader = inputReader; - _inputReader.SetInput(this); - } - public async Task GetSolutionPart1() - { - return string.Empty; - } - - public async Task GetSolutionPart2() - { - return string.Empty; - } - } -} \ No newline at end of file diff --git a/AdventOfCode.Solutions/2023/Day 00/day-00-input.txt b/AdventOfCode.Solutions/2023/Day 00/day-00-input.txt deleted file mode 100644 index e69de29..0000000 diff --git a/AdventOfCode.Solutions/2023/Day 17/Day17.cs b/AdventOfCode.Solutions/2023/Day 17/Day17.cs index 1795361..5a2e82a 100644 --- a/AdventOfCode.Solutions/2023/Day 17/Day17.cs +++ b/AdventOfCode.Solutions/2023/Day 17/Day17.cs @@ -1,4 +1,6 @@ -namespace AdventOfCode.Solutions._2023 +using AdventOfCode.Core.Shared.A_Star; + +namespace AdventOfCode.Solutions._2023 { public class Day17 : IChallange { @@ -15,6 +17,8 @@ public async Task GetSolutionPart1() { + AStarGrid grid = await _inputReader.ReadToGrid() as AStarGrid; + return string.Empty; } @@ -22,5 +26,25 @@ { return string.Empty; } + + public class HeatNode : AStarNode + { + private int HeatLoss { get => int.Parse("" + Value); } + + public HeatNode() { } + + public HeatNode(Point position) : base(position) + { + } + + public HeatNode(int x, int y, char value) : base(x, y, value) + { + } + + public override bool CanMoveTo(AStarNode target) + { + return base.CanMoveTo(target); + } + } } } \ No newline at end of file diff --git a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj index 679b1bb..7c30cae 100644 --- a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj +++ b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj @@ -7,6 +7,10 @@ enable + + + + @@ -30,7 +34,7 @@ Always - + Always @@ -72,6 +76,9 @@ Always + + Always + diff --git a/AdventOfCode.Solutions/day-00-input.txt b/AdventOfCode.Solutions/day-00-input.txt index 3c85086..dfca0b1 100644 --- a/AdventOfCode.Solutions/day-00-input.txt +++ b/AdventOfCode.Solutions/day-00-input.txt @@ -1,13 +1,6 @@ -2413432311323 -3215453535623 -3255245654254 -3446585845452 -4546657867536 -1438598798454 -4457876987766 -3637877979653 -4654967986887 -4564679986453 -1224686865563 -2546548887735 -4322674655533 \ No newline at end of file +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 \ No newline at end of file