started on astar code

This commit is contained in:
Rob 2023-11-27 21:50:58 +01:00
parent 0e5a63d58a
commit 2ad0676c49
10 changed files with 175 additions and 32 deletions

View File

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Advent Of Code Library\Advent Of Code Library.csproj" />
<ProjectReference Include="..\Advent Of Code Library\AdventOfCode.Library.csproj" />
</ItemGroup>
</Project>

View File

@ -3,37 +3,15 @@
using AdventOfCodeLibrary._2022;
using AdventOfCodeLibrary.Shared;
string _demoData = @"Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
string _demoData = @"Sabqponm
abcryxxl
accszExk
acctuvwj
abdefghi";
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1";
Answerable answerable = new Day11Part2();
Answerable answerable = new Day12Part1();
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
//dataArray = Encoding.UTF8.GetBytes(_demoData);
dataArray = Encoding.UTF8.GetBytes(_demoData);
Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}");
Console.ReadKey(true);

View File

@ -10,6 +10,20 @@
public override string GetAnswer(byte[] data)
{
int totalWidth = data.First(b => b == 10 || b == 13);
int totalHeigth = data.Count(b => b == 10);
int[][] map = new int[totalHeigth][];
for (int heigthIndex = 0; heigthIndex <= totalHeigth; heigthIndex++)
{
map[heigthIndex] = new int[totalWidth];
for (int widthIndex = 0; widthIndex <= totalWidth; widthIndex++)
{
}
}
return string.Empty;
}
}

View File

@ -0,0 +1,10 @@
namespace AdventOfCodeLibrary._2022.Day_12
{
internal class Map
{
internal void BuildMap(string[] mapData)
{
}
}
}

View File

@ -0,0 +1,46 @@
namespace AdventOfCodeLibrary.Shared.A_Star
{
public class AStarGrid : Grid<AStarNode>
{
const int TILE_MOVE_COST = 10;
public AStarGrid(AStarNode[] data) : base(data)
{
}
public int MinimalSteps(AStarNode start, AStarNode destination)
{
List<AStarNode> closedNodes = new();
List<AStarNode> openNodes = new List<AStarNode> { start };
do
{
AStarNode current = openNodes.OrderBy(n => n.FCost).First();
openNodes.Remove(current);
current.CloseNode();
closedNodes.Add(current);
if (current == destination)
{
//done
return 1;
}
var neighbors = GetNeighbors(current, false);
neighbors = neighbors.Where(n => current.CanMoveTo(n));
// calc costs
foreach(AStarNode neighbor in neighbors)
{
neighbor.ParentNode = current;
// since we only move straight the move cost is always the same
neighbor.GCost = current.GCost + TILE_MOVE_COST;
neighbor.HCost = neighbor.DistanceTo(destination) * TILE_MOVE_COST;
neighbor.OpenNode();
openNodes.Add(neighbor);
}
}
while (true);
}
}
}

View File

@ -0,0 +1,40 @@
namespace AdventOfCodeLibrary.Shared.A_Star
{
public class AStarNode : Node
{
/// <summary>
/// Distance from start node
/// </summary>
public int GCost { get; set; } = 0;
/// <summary>
/// Distance from end node
/// </summary>
public int HCost { get; set; }
/// <summary>
/// Total cost (G + F)
/// </summary>
public int FCost => GCost + HCost;
public bool? IsClosed { get; private set; } = null;
public AStarNode? ParentNode { get; set; }
public AStarNode(Node 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 bool CanMoveTo(AStarNode target)
{
int diff = target.Integer - Integer; ;
return diff == 0 || diff == 1;
}
}
}

View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace AdventOfCodeLibrary.Shared
{
public class Grid<T> where T : Node
{
public List<T> DataGrid { get; set; } = new List<T>();
public Grid(T[] data) => DataGrid.AddRange(data);
public Grid(IEnumerable<T> data) : this(data.ToArray()) { }
public IEnumerable<T> GetRow(int rowNumber) => DataGrid.Where(n => n.X == rowNumber);
public IEnumerable<T> GetColumn(int columnNumber) => DataGrid.Where(n => n.Y == columnNumber);
public IEnumerable<T> GetNeighbors(T source, bool allowDiagonals = true)
{
IEnumerable <T> neighbors = DataGrid.Where(target => Math.Abs(source.X - target.X) <= 1 || Math.Abs(source.Y - target.Y) <= 1);
if (allowDiagonals)
{
return neighbors;
}
return neighbors.Where(target => !(Math.Abs(source.X - target.X) <= 1 && Math.Abs(source.Y - target.Y) <= 1));
}
}
}

View File

@ -0,0 +1,25 @@
namespace AdventOfCodeLibrary.Shared
{
public class Node
{
internal int X { get; set; }
internal int Y { get; set; }
internal char Char { get; }
internal short Integer => (short)Char;
public Node(int x, int y, char character)
{
X = x;
Y = y;
Char = character;
}
public Node(Node position) : this(position.X, position.Y, position.Char)
{ }
public int DistanceTo(Node other)
{
return Math.Abs(X - other.X) + Math.Abs(Y - other.Y);
}
}
}

View File

@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33103.184
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Advent Of Code Runner", "Advend Of Code Runner\Advent Of Code Runner.csproj", "{6DCDC513-AF72-4029-932A-A0079BB5422B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Runner", "Advend Of Code Runner\AdventOfCode.Runner.csproj", "{6DCDC513-AF72-4029-932A-A0079BB5422B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Advent Of Code Library", "Advent Of Code Library\Advent Of Code Library.csproj", "{33CC3924-F18E-4B88-9989-A7A9077B9AC4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Library", "Advent Of Code Library\AdventOfCode.Library.csproj", "{33CC3924-F18E-4B88-9989-A7A9077B9AC4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution