Largest product in a grid: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 323: Line 323:
from math import prod
from math import prod


def maxproduct(mat, nlen):
def maxproduct(mat, length):
""" find the largest product of len length horizontal or vertical length in matrix """
""" find the largest product of len length horizontal or vertical length in matrix """
length, nrow, ncol = nlen - 1, len(mat), len(mat[0])
nrow, ncol = len(mat), len(mat[0])
maxprod, maxrow, maxcol, arr = 0, [0, 0], [0, 0], [0]
maxprod, maxrow, maxcol, arr = 0, [0, 0], [0, 0], [0]
for row in range(nrow):
for row in range(nrow):
for col in range(ncol):
for col in range(ncol):
row2, col2 = row + length + 1, col + length + 1
row2, col2 = row + length, col + length
if row < nrow - length + 1:
if row < nrow - length:
array = [r[col] for r in mat[row:row2]]
array = [r[col] for r in mat[row:row2]]
pro = prod(array)
pro = prod(array)
if pro > maxprod:
if pro > maxprod:
maxprod, maxrow, maxcol, arr = pro, [row, row2], col, array
maxprod, maxrow, maxcol, arr = pro, [row, row2], col, array
if col < ncol - length + 1:
if col < ncol - length:
pro = prod(mat[row][col:col2])
pro = prod(mat[row][col:col2])
if pro > maxprod:
if pro > maxprod:
maxprod, maxrow, maxcol, arr = pro, row, [col, col2], mat[row][col:col2]
maxprod, maxrow, maxcol, arr = pro, row, [col, col2], mat[row][col:col2]


print(f"The max {nlen}-product is {maxprod}, product of {arr} at row {maxrow}, col {maxcol}.")
print(f"The max {length}-product is {maxprod}, product of {arr} at row {maxrow}, col {maxcol}.")


MATRIX = [
MATRIX = [

Revision as of 21:55, 24 October 2021

Largest product in a grid is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

The task description is taken from Project Euler:
(https://projecteuler.net/problem=11)

Given the 20×20 grid below:


08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

What is the greatest product of four adjacent numbers in the same direction (down, right) in the 20×20 grid?


Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: grouping kernel math.matrices math.order prettyprint sequences ;

max-horizontal ( matrix m -- n )
   [ <clumps> ] curry map [ product ] matrix-map mmax ;
max-product ( matrix m -- n )
   [ dup flip ] dip [ max-horizontal ] curry bi@ max ;

{

   08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
   49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
   81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
   52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
   22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
   24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
   32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
   67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
   24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
   21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
   78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
   16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
   86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
   19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
   04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
   88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
   04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
   20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
   20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
   01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

} 20 group

4 max-product .</lang>

Output:
51267216

Go

Translation of: Wren
Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "rcu"
   "strings"

)

var grid = [][]int {

   { 8,  2, 22, 97, 38, 15,  0, 40,  0, 75,  4,  5,  7, 78, 52, 12, 50, 77, 91,  8},
   {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48,  4, 56, 62,  0},
   {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30,  3, 49, 13, 36, 65},
   {52, 70, 95, 23,  4, 60, 11, 42, 69, 24, 68, 56,  1, 32, 56, 71, 37,  2, 36, 91},
   {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
   {24, 47, 32, 60, 99,  3, 45,  2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
   {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
   {67, 26, 20, 68,  2, 62, 12, 20, 95, 63, 94, 39, 63,  8, 40, 91, 66, 49, 94, 21},
   {24, 55, 58,  5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
   {21, 36, 23,  9, 75,  0, 76, 44, 20, 45, 35, 14,  0, 61, 33, 97, 34, 31, 33, 95},
   {78, 17, 53, 28, 22, 75, 31, 67, 15, 94,  3, 80,  4, 62, 16, 14,  9, 53, 56, 92},
   {16, 39,  5, 42, 96, 35, 31, 47, 55, 58, 88, 24,  0, 17, 54, 24, 36, 29, 85, 57},
   {86, 56,  0, 48, 35, 71, 89,  7,  5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
   {19, 80, 81, 68,  5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77,  4, 89, 55, 40},
   { 4, 52,  8, 83, 97, 35, 99, 16,  7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
   {88, 36, 68, 87, 57, 62, 20, 72,  3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
   { 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18,  8, 46, 29, 32, 40, 62, 76, 36},
   {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74,  4, 36, 16},
   {20, 73, 35, 29, 78, 31, 90,  1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57,  5, 54},
   { 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52,  1, 89, 19, 67, 48},

}

func main() {

   maxProd, maxR1, maxR2, maxC1, maxC2 := 0, 0, 0, 0, 0
   var maxNums [4]int
   h, w := len(grid), len(grid[0])
   // right
   for r := 0; r < h; r++ {
       for c := 0; c < w-4; c++ {
           prod := 1
           for i := c; i < c+4; i++ {
               prod *= grid[r][i]
           }
           if prod > maxProd {
               maxProd = prod
               for n := 0; n < 4; n++ {
                   maxNums[n] = grid[r][c+n]
               }
               maxR1, maxR2 = r, r
               maxC1, maxC2 = c, c+3
           }
       }
   }
   // down
   for c := 0; c < w; c++ {
       for r := 0; r < h-4; r++ {
           prod := 1
           for i := r; i < r+4; i++ {
               prod *= grid[i][c]
           }
           if prod > maxProd {
               maxProd = prod
               for n := 0; n < 4; n++ {
                   maxNums[n] = grid[r+n][c]
               }
               maxR1, maxR2 = r, r+3
               maxC1, maxC2 = c, c
           }
       }
   }
   fmt.Println("The greatest product of four adjacent numbers in the same direction (down or right) in the grid is:")
   var maxNumStrs [4]string
   for i := 0; i < 4; i++ {
       maxNumStrs[i] = fmt.Sprintf("%d", maxNums[i])
   }
   fmt.Printf("  %s = %s\n", strings.Join(maxNumStrs[:], " x "), rcu.Commatize(maxProd))
   fmt.Print("  at indices (one based): ")
   for r := maxR1; r <= maxR2; r++ {
       for c := maxC1; c <= maxC2; c++ {
           fmt.Printf("(%d, %d) ", r+1, c+1)
       }
   }
   fmt.Println()

}</lang>

Output:
The greatest product of four adjacent numbers in the same direction (down or right) in the grid is:
  66 x 91 x 88 x 97 = 51,267,216
  at indices (one based): (7, 16) (8, 16) (9, 16) (10, 16) 

Julia

First, a quick method, which does not reveal the product locations: <lang julia>mat = [

   08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
   49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
   81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
   52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
   22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
   24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
   32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
   67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
   24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
   21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
   78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
   16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
   86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
   19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
   04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
   88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
   04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
   20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
   20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
   01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

]

x = max(maximum([prod(mat[j, i:i+3]) for i in 1:17, j in 1:20]),

   maximum([prod(mat[i:i+3, j]) for i in 1:17, j in 1:20]))

println("The maximum product of 4 adjacent horizontal or vertical in the matrix is: $x")

</lang>

Output:
The maximum product of 4 adjacent horizontal or vertical in the matrix is: 51267216

Alternatively, to get the position of the maximum product: <lang julia>function maxprod(mat, len)

   nrow, ncol = size(mat)
   maxprod, maxrow, maxcol, arr = 0, 0:0, 0:0, [0]
   for row in 1:nrow, col in 1:ncol
       if row < nrow - len + 2
           pro = prod(mat[row:row+len-1, col])
           if pro > maxprod
               maxprod, maxrow, maxcol, arr = pro, row:row+len-1, col:col, mat[row:row+len-1, col]
           end
       end
       if col < ncol - len + 2
           pro = prod(mat[row, col:col+len-1])
           if pro > maxprod
               maxprod, maxrow, maxcol, arr = pro, row:row, col:col+len-1, mat[row, col:col+len-1]
           end
       end
   end
   println("The maximum product is $maxprod, product of $arr at row $maxrow, col $maxcol")

end

maxprod(mat, 4)

</lang>

Output:
The maximum product is 51267216, product of [66, 91, 88, 97] at row 7:10, col 16:16

Perl

<lang perl>#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Largest_product_in_a_grid use warnings; use List::Util qw( max );

$_ = <<END; 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 END

my $score = 0; for my $gap ( qr/ /, qr/.{58}/s )

 {
 $score = max $score, $1 * $2 * $3 * $4
   while /(?=(\d\d)$gap(\d\d)$gap(\d\d)$gap(\d\d))/g;
 }

print "max is $score\n";</lang>

Output:
max is 51267216

Generalized

Handles non-square input (both narrow and wide). <lang perl>use strict; use warnings; use feature 'say'; use List::AllUtils <max reduce>;

my $input = <<~END;

 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
 END

my(@m,@mt); push @m, [ split /\s+/, s/\b0//gr ] for split "\n", $input; for my $j (0..$#{$m[0]}) { push @mt, [ map $_->[$j], @m ] } # transpose

sub max_products {

   my($terms,@matrix) = @_;
   my @products;
   my $columns = 1 + $#{$matrix[0]};
   for my $row (@matrix) {
       map { push @products, reduce { $a * $b } @$row[ $_ .. $_+$terms-1 ] } 0 .. $columns-$terms;
   }
   max @products

}

say "Largest product of $_ adjacent elements: " . max max_products($_,@m), max_products($_,@mt) for 1..6;</lang>

Output:
Largest product of 1 adjacent elements: 99
Largest product of 2 adjacent elements: 9215
Largest product of 3 adjacent elements: 776776
Largest product of 4 adjacent elements: 51267216
Largest product of 5 adjacent elements: 2326829868
Largest product of 6 adjacent elements: 188210512710

Python

Translation of: Julia

<lang python>""" Rosetta code task: Largest_product_in_a_grid """

from math import prod

def maxproduct(mat, length):

   """ find the largest product of len length horizontal or vertical length in matrix """
   nrow, ncol = len(mat), len(mat[0])
   maxprod, maxrow, maxcol, arr = 0, [0, 0], [0, 0], [0]
   for row in range(nrow):
       for col in range(ncol):
           row2, col2 = row + length, col + length
           if row < nrow - length:
               array = [r[col] for r in mat[row:row2]]
               pro = prod(array)
               if pro > maxprod:
                   maxprod, maxrow, maxcol, arr = pro, [row, row2], col, array
           if col < ncol - length:
               pro = prod(mat[row][col:col2])
               if pro > maxprod:
                   maxprod, maxrow, maxcol, arr = pro, row, [col, col2], mat[row][col:col2]
   print(f"The max {length}-product is {maxprod}, product of {arr} at row {maxrow}, col {maxcol}.")

MATRIX = [

   [ 8,  2, 22, 97, 38, 15,  0, 40,  0, 75,  4,  5,  7, 78, 52, 12, 50, 77, 91,  8],
   [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48,  4, 56, 62,  0],
   [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30,  3, 49, 13, 36, 65],
   [52, 70, 95, 23,  4, 60, 11, 42, 69, 24, 68, 56,  1, 32, 56, 71, 37,  2, 36, 91],
   [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
   [24, 47, 32, 60, 99,  3, 45,  2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
   [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
   [67, 26, 20, 68,  2, 62, 12, 20, 95, 63, 94, 39, 63,  8, 40, 91, 66, 49, 94, 21],
   [24, 55, 58,  5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
   [21, 36, 23,  9, 75,  0, 76, 44, 20, 45, 35, 14,  0, 61, 33, 97, 34, 31, 33, 95],
   [78, 17, 53, 28, 22, 75, 31, 67, 15, 94,  3, 80,  4, 62, 16, 14,  9, 53, 56, 92],
   [16, 39,  5, 42, 96, 35, 31, 47, 55, 58, 88, 24,  0, 17, 54, 24, 36, 29, 85, 57],
   [86, 56,  0, 48, 35, 71, 89,  7,  5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
   [19, 80, 81, 68,  5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77,  4, 89, 55, 40],
   [ 4, 52,  8, 83, 97, 35, 99, 16,  7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
   [88, 36, 68, 87, 57, 62, 20, 72,  3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
   [ 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18,  8, 46, 29, 32, 40, 62, 76, 36],
   [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74,  4, 36, 16],
   [20, 73, 35, 29, 78, 31, 90,  1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57,  5, 54],
   [ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52,  1, 89, 19, 67, 48]

]

for n in range(2, 6):

   maxproduct(MATRIX, n)

</lang>

Output:
The max 2-product is 9215, product of [95, 97] at row [7, 9], col 8.
The max 3-product is 776776, product of [91, 88, 97] at row [7, 10], col 15.
The max 4-product is 51267216, product of [66, 91, 88, 97] at row [6, 10], col 15.
The max 5-product is 2326829868, product of [62, 99, 69, 82, 67] at row 17, col [9, 14].

Raku

General solution. No hard coded values. Works with any size matrix, configurable number of terms. <lang perl6>my @matrix = q:to/END/.lines».words;

 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 

END

my $terms = 4;

say "Largest product of $terms adjacent elements: " ~ max flat (^@matrix).map: {

   @matrix.rotor($terms => -$terms+1).flat»[$_].batch($terms)».reduce(&[*]), # vertical
   @matrix[$_].rotor($terms => -$terms+1)».reduce(&[*]);                     # horizontal

}</lang>

Output:
Largest product of 4 adjacent elements: 51267216

Ring

<lang ring> see "working..." + nl see "Largest product is:" + nl

Grid = [[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],

       [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
       [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
       [52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
       [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
       [24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
       [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
       [67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
       [24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
       [21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],
       [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92],
       [16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],
       [86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
       [19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40],
       [04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
       [88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
       [04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36],
       [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16],
       [20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54],
       [01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48]]

Index = [] resTemp = [] prodNew = 0

for n = 1 to 17

   prod = 0
   for m = 1 to 20
       prod = Grid[n][m] * Grid[n+1][m] * Grid[n+2][m] * Grid[n+3][m]
       if prod > prodNew
          prodNew = prod 
          res = 1000*Grid[n][m] + 100*Grid[n+1][m] + 10*Grid[n+2][m] + Grid[n+3][m]
          resTemp = []
          Index = []
          add(Index,[n,m])
          add(Index,[n+1,m])
          add(Index,[n+2,m])
          add(Index,[n+3,m])           
          add(resTemp,Grid[n][m])
          add(resTemp,Grid[n+1][m])
          add(resTemp,Grid[n+2][m])
          add(resTemp,Grid[n+3][m])
       ok
   next

next

for n = 20 to 1 step -1

   prod = 0
   for m = 1 to 17
       prod = Grid[n][m] * Grid[n][m+1] * Grid[n][m+2] * Grid[n][m+3]
       if prod > prodNew
          prodNew = prod 
          res = 1000*Grid[n][m] + 100*Grid[n][m+1] + 10*Grid[n][m+2] + Grid[n][m+3]
          resTemp = []
          Index = []
          add(Index,[n,m])
          add(Index,[n,m+1])
          add(Index,[n,m+2])
          add(Index,[n,m+3])
          resTemp = []
          add(resTemp,Grid[n][m])
          add(resTemp,Grid[n][m+1])
          add(resTemp,Grid[n+2][m+2])
          add(resTemp,Grid[n][m+3])
       ok
   next

next

for n = 1 to len(resTemp)-1

   see "" + resTemp[n] + " * "

next see "" + resTemp[len(resTemp)] + " = " + prodNew + nl

see "Indices = " for n = 1 to len(Index)

   see "(" + Index[n][1] + "," + Index[n][2] + ")"

next

see nl + "done..." + nl </lang>

Output:
working...
Largest product is:
66 * 91 * 88 * 97 = 51267216
Indices = (7,16)(8,16)(9,16)(10,16)
done...

Wren

Library: Wren-fmt

<lang ecmascript>import "./fmt" for Fmt

var grid = [

   [08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
   [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
   [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
   [52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
   [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
   [24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
   [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
   [67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
   [24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
   [21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],
   [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92],
   [16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],
   [86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
   [19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40],
   [04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
   [88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
   [04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36],
   [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16],
   [20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54],
   [01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48]

]

var maxProd = 0 var maxNums = [0, 0, 0, 0] var maxR1 = 0 var maxR2 = 0 var maxC1 = 0 var maxC2 = 0 var h = grid.count var w = grid[0].count

// right for (r in 0...h) {

   for (c in 0..w-5) {
       var prod = 1
       for (i in c..c+3) prod = prod * grid[r][i]
       if (prod > maxProd) {
           maxProd = prod
           for (n in 0..3) maxNums[n] = grid[r][c+n]
           maxR1 = maxR2 = r
           maxC1 = c
           maxC2 = c + 3
       }
   }

}

// down for (c in 0...w) {

   for (r in 0..h-5) {
       var prod = 1
       for (i in r..r+3) prod = prod * grid[i][c]
       if (prod > maxProd) {
           maxProd = prod
           for (n in 0..3) maxNums[n] = grid[r+n][c]
           maxR1 = r
           maxR2 = r + 3
           maxC1 = maxC2 = c
       }
   }

}

System.print("The greatest product of four adjacent numbers in the same direction (down or right) in the grid is:") Fmt.print(" $s = $,d", maxNums.map{ |n| n.toString }.join(" x "), maxProd) System.write(" at indices (one based): ") for (r in maxR1..maxR2) {

   for (c in maxC1..maxC2) Fmt.write("($d, $d) ", r+1, c+1)

} System.print()</lang>

Output:
The greatest product of four adjacent numbers in the same direction (down or right) in the grid is:
  66 x 91 x 88 x 97 = 51,267,216
  at indices (one based): (7, 16) (8, 16) (9, 16) (10, 16)