In the 20 by 20 grid below, five numbers along a diagonal line have been marked in bold.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _2008_2_CodingChallenge
{
class Program
{
static void Main(string[] args)
{
long currentProduct = long.MaxValue;
List<CellItem> currentList = new List<CellItem>();
int[,] numbers = {
{47,90,41,82,1,96,95,27,50,91,97,65,49,38,96,90,90,90,84,27 },
{6,35,42,36,25,31,20,57,86,61,34,6,73,13,59,72,55,51,72,53 },
{20,2,44,25,28,57,5,29,21,12,12,30,20,72,40,33,32,14,93,24 },
{95,3,96,2,77,77,96,16,9,92,85,36,18,52,5,49,70,39,62,53 },
{33,1,47,74,50,5,65,84,57,60,64,80,13,40,74,90,33,82,49,49 },
{10,61,2,69,70,71,45,43,33,83,8,56,9,69,86,67,80,17,65,76 },
{23,31,36,20,81,60,53,36,64,86,68,94,2,68,73,14,50,37,21,49 },
{4,60,79,87,2,28,58,58,49,59,19,50,74,83,52,18,61,2,93,88 },
{98,52,76,5,30,34,32,85,3,10,39,60,26,51,50,69,36,21,48,99 },
{5,85,47,66,69,27,83,5,34,79,28,59,32,68,5,84,15,58,54,25 },
{13,13,18,80,92,33,88,7,61,63,93,39,33,67,15,24,6,8,18,97 },
{60,19,98,51,98,71,65,23,39,18,90,26,59,90,90,2,4,31,34,59 },
{31,56,94,13,12,37,71,88,19,97,79,70,51,95,54,67,55,16,80,81},
{64,92,17,24,51,48,87,36,82,63,41,50,25,56,84,94,13,34,86,82},
{5,51,11,83,78,91,88,99,61,84,54,91,77,25,44,75,79,46,6,6 },
{31,38,58,16,36,46,66,57,24,77,16,61,23,88,79,79,19,82,31,37},
{98,86,7,15,69,50,90,77,32,65,84,1,36,44,57,66,38,11,68,45 },
{32,38,96,61,47,36,43,70,32,36,15,34,7,90,70,96,95,7,29,11 },
{27,29,4,44,41,89,30,65,50,14,60,37,49,6,69,17,22,23,32,95 },
{93,62,98,22,20,33,27,1,97,17,93,92,8,38,9,78,20,51,13,18 }
};
List<CellItem> cellItems = new List<CellItem>();
for (int i = 0; i < numbers.GetLength(0); i++)
for (int j = 0; j < numbers.GetLength(1); j++)
cellItems.Add(new CellItem { Row = i, Col = j, Value = numbers[i, j] });
var down =
from row in Enumerable.Range(0, 15)
from col in Enumerable.Range(0, 19)
select (from offset in Enumerable.Range(0, 5)
from ci in cellItems
where ci.Row == row + offset && ci.Col == col
select ci );
var left =
from row in Enumerable.Range(0, 19)
from col in Enumerable.Range(0, 15)
select (from offset in Enumerable.Range(0, 5)
from ci in cellItems
where ci.Row == row && ci.Col == col + offset
select ci);
var diag =
from row in Enumerable.Range(0, 15)
from col in Enumerable.Range(0, 15)
select (from offset in Enumerable.Range(0, 5)
from ci in cellItems
where ci.Row == row + offset && ci.Col == col + offset
select ci);
var combined = down.Concat(left).Concat(diag);
combined.ToList().ForEach(delegate(IEnumerable<CellItem> items)
{
if (currentProduct > items.CalculateProduct())
{
currentProduct = items.CalculateProduct();
currentList = items.ToList();
}
});
currentList.ConsoleDump();
Console.ReadKey();
}
}
public static class Extensions
{
public static long CalculateProduct(this IEnumerable<CellItem> items)
{
long product = 1;
foreach (CellItem item in items)
product *= item.Value;
return product;
}
public static void ConsoleDump(this IEnumerable<CellItem> items)
{
foreach (CellItem item in items)
Console.WriteLine(string.Format("row = {0} col = {1} value = {2}",
item.Row, item.Col, item.Value));
Console.Write(string.Format("product = {0}", items.CalculateProduct()));
Console.WriteLine();
}
}
public class CellItem
{
public int Row { get; set; }
public int Col { get; set; }
public int Value { get; set; }
}
}
Yair submitted another Excel VBA solution with added flexibility for defining the problem matrix size and simpler looping.