private static int GetRiskSum(string Input) { var matrix = ParseInput(Input); var riskSum = 0; foreach (var(x, y, value) in matrix.EveryPointIn()) { var everyAdjacentIsLower = !(MatrixExtensions.IsInsideBounds(matrix, x + 1, y) && matrix[x + 1, y] <= value) && !(MatrixExtensions.IsInsideBounds(matrix, x, y + 1) && matrix[x, y + 1] <= value) && !(MatrixExtensions.IsInsideBounds(matrix, x - 1, y) && matrix[x - 1, y] <= value) && !(MatrixExtensions.IsInsideBounds(matrix, x, y - 1) && matrix[x, y - 1] <= value); if (everyAdjacentIsLower) { riskSum += value + 1; } } return(riskSum); }
public void TestIsInsideBounds() { var matrix = new[, ] { { 0, 1 } }; Assert.That(matrix[0, 1], Is.EqualTo(1)); var width = matrix.GetLength(0); Assert.That(width, Is.EqualTo(1)); var height = matrix.GetLength(1); Assert.That(height, Is.EqualTo(2)); Assert.That(MatrixExtensions.IsInsideBounds(matrix, 0, 1), Is.True); Assert.That(MatrixExtensions.IsInsideBounds(matrix, -1, 0), Is.False); Assert.That(MatrixExtensions.IsInsideBounds(matrix, 0, -1), Is.False); Assert.That(MatrixExtensions.IsInsideBounds(matrix, 1, 0), Is.False); Assert.That(MatrixExtensions.IsInsideBounds(matrix, 0, 2), Is.False); }