using System.Collections.Generic; namespace AdventOfCode.Core.Shared { public class Grid where T : Node { public List DataGrid { get; set; } = new List(); 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)); } } }