You are viewing a single comment's thread. Return to all comments →
using System;
class Solution { static void Main() { int[,] grid = ParseInputGrid();
int maxProduct = FindMaxProduct(grid); Console.WriteLine(maxProduct); } static int[,] ParseInputGrid() { int[,] grid = new int[20, 20]; for (int i = 0; i < 20; i++) { string[] row = Console.ReadLine().Split(' '); for (int j = 0; j < 20; j++) { grid[i, j] = int.Parse(row[j]); } } return grid; } static int FindMaxProduct(int[,] grid) { int maxProduct = 0; // Check horizontally for (int i = 0; i < 20; i++) { for (int j = 0; j <= 16; j++) { int product = grid[i, j] * grid[i, j + 1] * grid[i, j + 2] * grid[i, j + 3]; maxProduct = Math.Max(maxProduct, product); } } // Check vertically for (int i = 0; i <= 16; i++) { for (int j = 0; j < 20; j++) { int product = grid[i, j] * grid[i + 1, j] * grid[i + 2, j] * grid[i + 3, j]; maxProduct = Math.Max(maxProduct, product); } } // Check diagonally (top-left to bottom-right) for (int i = 0; i <= 16; i++) { for (int j = 0; j <= 16; j++) { int product = grid[i, j] * grid[i + 1, j + 1] * grid[i + 2, j + 2] * grid[i + 3, j + 3]; maxProduct = Math.Max(maxProduct, product); } } // Check diagonally (top-right to bottom-left) for (int i = 0; i <= 16; i++) { for (int j = 3; j < 20; j++) { int product = grid[i, j] * grid[i + 1, j - 1] * grid[i + 2, j - 2] * grid[i + 3, j - 3]; maxProduct = Math.Max(maxProduct, product); } } return maxProduct; }
}
Project Euler #11: Largest product in a grid
You are viewing a single comment's thread. Return to all comments →
using System;
class Solution { static void Main() { int[,] grid = ParseInputGrid();
}