using System.Text; namespace AOC.Helpers; public static class TwoDimensionalArrays { public static char[,] Make2DArrayFromStringArray(string[] stringArray) { int rows = stringArray.Length; int cols = stringArray[0].Length; char[,] array = new char[rows, cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { array[r, c] = stringArray[r][c]; } } return array; } public static string TwoDimensionalArrayToString(char[,] array) { int rows = array.GetLength(0); int cols = array.GetLength(1); StringBuilder sb = new(rows * (cols + 1)); // performance optimisation for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { sb.Append(array[r, c]); } sb.AppendLine(); // end of row } return sb.ToString(); } /// /// Enumerates all cells in a 2D array, returning each cell's row index, /// column index, and value. /// /// The element type of the 2D array. /// The 2D array to enumerate. /// /// An of tuples containing the row index, /// column index, and value of each cell in the array, iterated in row-major order. /// /// /// The following example prints every cell in a character grid: /// /// char[,] grid = { /// { 'A', 'B', 'C' }, /// { 'D', 'E', 'F' } /// }; /// /// foreach (var (row, column, value) in Cells(grid)) /// { /// Console.WriteLine($"[{row}, {column}] = {value}"); /// } /// /// This produces: /// /// [0, 0] = A /// [0, 1] = B /// [0, 2] = C /// [1, 0] = D /// [1, 1] = E /// [1, 2] = F /// /// public static IEnumerable<(int row, int column, T value)> Cells(T[,] grid) { int rows = grid.GetLength(0); int cols = grid.GetLength(1); for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { yield return (r, c, grid[r, c]); } } } public static List<(int row, int column)> CheckAllSurroundingCellsForCharacter( char[,] array, int startRow, int startColumn, char characterToCheckFor) { int totalRows = array.GetLength(0); int totalColumns = array.GetLength(1); List<(int row, int column)> results = new(); for (int rowOffset = -1; rowOffset <= 1; rowOffset++) { for (int colOffset = -1; colOffset <= 1; colOffset++) { // Skip the centre cell if (rowOffset == 0 && colOffset == 0) { continue; } int row = startRow + rowOffset; int column = startColumn + colOffset; // Bounds check if (row < 0 || row >= totalRows || column < 0 || column >= totalColumns) { continue; } if (array[row, column] == characterToCheckFor) { results.Add((row, column)); } } } return results; } }