Ejemplo n.º 1
0
        public static void PrintWithResult(int[][] grid, int numberOfFactors, ProductResult productResult)
        {
            var sb = new StringBuilder();
            int x  = productResult.Horizontal - 1;
            int y  = productResult.Vertical - 1;
            int countOfMarkedItems = 0;

            for (int i = 0; i < grid.Length; i++)
            {
                for (int j = 0; j < grid[i].Length; j++)
                {
                    if (x == j && y == i && countOfMarkedItems < numberOfFactors)
                    {
                        sb.Append(string.Format("{0, -5}", "!" + grid[i][j]));
                        switch (productResult.Direction)
                        {
                        case Directions.Horizontal:
                            x++;
                            break;

                        case Directions.Vertical:
                            y++;
                            break;

                        case Directions.ForwardDiagonal:
                            x--;
                            y++;
                            break;

                        case Directions.BackwardDiagonal:
                            x++;
                            y++;
                            break;

                        default:
                            break;
                        }
                        countOfMarkedItems++;
                    }
                    else
                    {
                        sb.Append(string.Format("{0, -5}", grid[i][j]));
                    }
                }



                sb.Append("\n");
            }

            Console.WriteLine(sb.ToString());
            Console.WriteLine($"With greatest product of {string.Format("{0:n0}", productResult.Product)}");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public async Task <ProductResult> CheckHorizontal()
        {
            ProductResult retVal = await Task.Run(() =>
            {
                int greatestHorizontalProduct = int.MinValue;
                int x = int.MinValue;
                int y = int.MinValue;

                for (int i = 0; i < _grid.Length; i++)
                {
                    for (int j = 0; j < _grid[i].Length - _numberOfFactors + 1; j++)
                    {
                        int product = 1;
                        for (int k = 0; k < _numberOfFactors; k++)
                        {
                            product *= _grid[i][j + k];
                        }

                        if (product > greatestHorizontalProduct)
                        {
                            greatestHorizontalProduct = product;
                            x = j + 1;
                            y = i + 1;
                        }
                    }
                }

                var result = new ProductResult
                {
                    Product    = greatestHorizontalProduct,
                    Horizontal = x,
                    Vertical   = y,
                    Direction  = Directions.Horizontal
                };

                return(result);
            }).ConfigureAwait(false);


            return(retVal);
        }