namespace AdventOfCode.Core.Shared { public class Grid where T : Node { public List DataGrid { get; set; } = []; public int Columns => DataGrid.Max(n => n.X); public int Rows => DataGrid.Max(n => n.Y); 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 T GetNode(int x, int y) => DataGrid.First(n => n.X == x && n.Y == y); public IEnumerable FindWithValue(char toFind) => DataGrid.Where(n => n.Char == toFind); public void UpdateNode(T node) { T gridNode = DataGrid.First(n => n.X == node.X && n.Y == node.Y); int nodeLocation = DataGrid.IndexOf(gridNode); DataGrid[nodeLocation] = node; } 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 && source.Y == target.Y) || (source.X == target.X && 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); } } }