using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace _2021___Day_09 { class Program { static void Main(string[] args) { var input = File.ReadAllLines("../../../../Input.in"); var grid = new int[input.Length, input[0].Length]; var Basins = new List<int>(); for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { grid[i, j] = int.Parse(input[i][j].ToString()); } } for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { int up = 10; int down = 10; int right = 10; int left = 10; if (i > 0) up = grid[i - 1, j]; if (i < grid.GetLength(0) - 1) down = grid[i + 1, j]; if (j > 0) left = grid[i, j - 1]; if (j < grid.GetLength(1) - 1) right = grid[i, j + 1]; if (grid[i, j] < up && grid[i, j] < down && grid[i, j] < left && grid[i, j] < right) { Basins.Add(grid[i, j]); } } } Console.WriteLine("The risk level of all low points is " + Basins.Sum() + Basins.Count); var basins = new List<int>(); for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { grid[i, j] = int.Parse(input[i][j].ToString()); } } for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { int up = 10; int down = 10; int right = 10; int left = 10; if (i > 0) up = grid[i - 1, j]; if (i < grid.GetLength(0) - 1) down = grid[i + 1, j]; if (j > 0) left = grid[i, j - 1]; if (j < grid.GetLength(1) - 1) right = grid[i, j + 1]; if (grid[i, j] < up && grid[i, j] < down && grid[i, j] < left && grid[i, j] < right) { basins.Add(BasinSize(grid, i, j)); } } basins.Sort(); basins.Reverse(); } Console.WriteLine("The product of all three sizes is " + basins[0] * basins[1] * basins[2]); } private static int BasinSize(int[,] grid, int y, int x) { if (grid[y, x] == 9) return 0; grid[y, x] = -1; int size = 1; if (x > 0 && grid[y, x - 1] > -1 && grid[y, x - 1] < 9) size += BasinSize(grid, y, x - 1); if (y > 0 && grid[y - 1, x] > -1 && grid[y - 1, x] < 9) size += BasinSize(grid, y - 1, x); if (y < grid.GetLength(0) - 1 && grid[y + 1, x] > -1 && grid[y + 1, x] < 9) size += BasinSize(grid, y + 1, x); if (x < grid.GetLength(1) - 1 && grid[y, x + 1] > -1 && grid[y, x + 1] < 9) size += BasinSize(grid, y, x + 1); return size; } } }