namespace AdventOfCode.Core.Shared { public class Grid where T : Node { public List DataGrid { get; set; } = []; public Grid() { } public Grid(T[] data) => DataGrid.AddRange(data); public Grid(IEnumerable data) : this(data.ToArray()) { } public IEnumerable GetRow(int rowNumber) => DataGrid.Where(n => n.X == rowNumber); public IEnumerable GetColumn(int columnNumber) => DataGrid.Where(n => n.Y == columnNumber); public IEnumerable GetNeighbors(T source, bool allowDiagonals = true) { IEnumerable 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)); } public IEnumerable GetSection(int fromX, int fromY, int toX, int toY) { return DataGrid.Where(node => node.X >= fromX && node.X <= toX && node.Y >= fromY && node.Y <= toY); } } }