AdventOfCode/Advent Of Code Library/Shared/Grid.cs
2023-11-27 21:50:58 +01:00

31 lines
984 B
C#

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));
}
}
}