worked in A*, completed day 01, 2024
This commit is contained in:
parent
97d6a79643
commit
0555dad92a
@ -8,6 +8,11 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdventOfCode.Solutions\AdventOfCode.Solutions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -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<SolutionManager>()
|
||||
.GetChallange(2024, 1);
|
||||
|
||||
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>();
|
||||
}
|
||||
@ -1,20 +1,19 @@
|
||||
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 MinimalSteps(AStarNode start, AStarNode destination)
|
||||
public int MinimalSteps(T start, T destination)
|
||||
{
|
||||
List<AStarNode> openNodes = [start];
|
||||
List<AStarNode> closedNodes = [];
|
||||
List<T> openNodes = [start];
|
||||
List<T> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,18 +19,14 @@ namespace AdventOfCode.Core.Shared.A_Star
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<string> GetSolutionPart1()
|
||||
{
|
||||
AStarGrid<HeatNode> grid = await _inputReader.ReadToGrid<HeatNode>() as AStarGrid<HeatNode>;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdventOfCode.Core\AdventOfCode.Core.csproj" />
|
||||
</ItemGroup>
|
||||
@ -30,7 +34,7 @@
|
||||
<Compile Update="2023\Day 13\Day13.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2023\Day 00\Day00.cs">
|
||||
<Compile Update="Day 00\Day00.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2023\Day 12\Day12.cs">
|
||||
@ -72,6 +76,9 @@
|
||||
<Compile Update="2023\Day 03\Day03.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2024\Day 01\Day01.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,13 +1,6 @@
|
||||
2413432311323
|
||||
3215453535623
|
||||
3255245654254
|
||||
3446585845452
|
||||
4546657867536
|
||||
1438598798454
|
||||
4457876987766
|
||||
3637877979653
|
||||
4654967986887
|
||||
4564679986453
|
||||
1224686865563
|
||||
2546548887735
|
||||
4322674655533
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
Loading…
Reference in New Issue
Block a user