Example #1
0
        public static Result LargestProductInGrid(Problem arguments)
        {
            string   sequence = arguments.Sequence;
            int      adjacent = Convert.ToInt32(arguments.LongNumber);
            int      maxProduct = int.MinValue, product = 0;
            CellList VisitedCellBlocks = new CellList(); //so we don't calculate on repeated blocks of adjcent cells

            //initialize grid from string sequence
            string[][] grid = sequence.Split(':').Select(row => row.Split(',')).ToArray();
            int        size = grid.Length;

            //get highest adajacent product of adjacents to every grid cell
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    product = UtilityMath.GetAdjacentMaxproduct(i, j, adjacent, size, grid, VisitedCellBlocks);

                    if (product > maxProduct)
                    {
                        maxProduct = product;
                    }
                }
            }

            var m = string.Format("The greatest product of {0} adjacent numbers in the same direction (up, down, left, right, or diagonally) in the {1}×{1} grid is {2}.", adjacent, size, maxProduct);
            var r = new Result(arguments.Id, m);

            return(r);
        }