worked in A*, completed day 01, 2024

This commit is contained in:
Rob 2024-12-01 09:39:04 +01:00
parent 97d6a79643
commit 0555dad92a
9 changed files with 90 additions and 68 deletions

View File

@ -8,6 +8,11 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AdventOfCode.Solutions\AdventOfCode.Solutions.csproj" /> <ProjectReference Include="..\AdventOfCode.Solutions\AdventOfCode.Solutions.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,14 +1,27 @@
using AdventOfCode.Solutions._2023; using AdventOfCode.Solutions;
using AdventOfCode.Core.Shared; using AdventOfCode.Core.Shared;
using AdventOfCode.Core; 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<SolutionManager>()
.GetChallange(2024, 1);
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}"); Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
Console.WriteLine($"Part 2: {await challange.GetSolutionPart2()}"); Console.WriteLine($"Part 2: {await challange.GetSolutionPart2()}");
static void ConfigureServices(IServiceCollection services)
{
services
.AddChallanges()
.AddTransient<InputReader, InputReader>()
.AddScoped<SolutionManager>();
}

View File

@ -1,20 +1,19 @@
namespace AdventOfCode.Core.Shared.A_Star namespace AdventOfCode.Core.Shared.A_Star
{ {
public class AStarGrid(AStarNode[] data) : Grid<AStarNode>(data) public class AStarGrid<T>(T[] data) : Grid<T>(data) where T : AStarNode
{ {
public int TileMoveCost { get; set; } public int TileMoveCost { get; set; }
public int MinimalSteps(AStarNode start, AStarNode destination) public int MinimalSteps(T start, T destination)
{ {
List<AStarNode> openNodes = [start]; List<T> openNodes = [start];
List<AStarNode> closedNodes = []; List<T> closedNodes = [];
while(openNodes.Count != 0) while(openNodes.Count != 0)
{ {
// get the lowest F-cost node // 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); openNodes.Remove(current);
current.CloseNode();
closedNodes.Add(current); closedNodes.Add(current);
if (current == destination) if (current == destination)
@ -23,22 +22,33 @@
return 1; return 1;
} }
var neighbors = GetNeighbors(current, false); var neighbors = GetNeighbors(current, false)
neighbors = neighbors.Where(n => current.CanMoveTo(current)); .Where(n => current.CanMoveTo(current));
// calc costs // calc costs
foreach(AStarNode neighbor in neighbors) foreach(T neighbor in neighbors)
{ {
neighbor.ParentNode = current; long g = current.GCost + GetMoveCost(current, neighbor);
// since we only move straight the move cost is always the same long h = neighbor.GetManhattanDistance(destination) * GetMoveCost(current, neighbor);
neighbor.GCost = current.GCost + TileMoveCost; long f = g + h;
neighbor.HCost = neighbor.GetManhattanDistance(destination) * TileMoveCost;
neighbor.OpenNode(); 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); openNodes.Add(neighbor);
} }
} }
return -1; return -1;
} }
public virtual int GetMoveCost(T current, T neighbor) => TileMoveCost;
} }
} }

View File

@ -19,18 +19,14 @@ namespace AdventOfCode.Core.Shared.A_Star
/// </summary> /// </summary>
public long FCost => GCost + HCost; public long FCost => GCost + HCost;
public bool? IsClosed { get; private set; } = null;
public AStarNode? ParentNode { get; set; } public AStarNode? ParentNode { get; set; }
public AStarNode() { }
public AStarNode(Point position) : base(position) { } public AStarNode(Point position) : base(position) { }
public AStarNode(int x, int y, char value) : base(x, y, value) { } 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; public virtual bool CanMoveTo(AStarNode target) => true;
} }
} }

View File

@ -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<string> GetSolutionPart1()
{
return string.Empty;
}
public async Task<string> GetSolutionPart2()
{
return string.Empty;
}
}
}

View File

@ -1,4 +1,6 @@
namespace AdventOfCode.Solutions._2023 using AdventOfCode.Core.Shared.A_Star;
namespace AdventOfCode.Solutions._2023
{ {
public class Day17 : IChallange public class Day17 : IChallange
{ {
@ -15,6 +17,8 @@
public async Task<string> GetSolutionPart1() public async Task<string> GetSolutionPart1()
{ {
AStarGrid<HeatNode> grid = await _inputReader.ReadToGrid<HeatNode>() as AStarGrid<HeatNode>;
return string.Empty; return string.Empty;
} }
@ -22,5 +26,25 @@
{ {
return string.Empty; 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);
}
}
} }
} }

View File

@ -7,6 +7,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AdventOfCode.Core\AdventOfCode.Core.csproj" /> <ProjectReference Include="..\AdventOfCode.Core\AdventOfCode.Core.csproj" />
</ItemGroup> </ItemGroup>
@ -30,7 +34,7 @@
<Compile Update="2023\Day 13\Day13.cs"> <Compile Update="2023\Day 13\Day13.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile> </Compile>
<Compile Update="2023\Day 00\Day00.cs"> <Compile Update="Day 00\Day00.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile> </Compile>
<Compile Update="2023\Day 12\Day12.cs"> <Compile Update="2023\Day 12\Day12.cs">
@ -72,6 +76,9 @@
<Compile Update="2023\Day 03\Day03.cs"> <Compile Update="2023\Day 03\Day03.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile> </Compile>
<Compile Update="2024\Day 01\Day01.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,13 +1,6 @@
2413432311323 3 4
3215453535623 4 3
3255245654254 2 5
3446585845452 1 3
4546657867536 3 9
1438598798454 3 3
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533