How to find max product of 4 adjacent numbers in the same direction in a 20×20 grid with TypeScript

1 Answer

0 votes
function printAlignedGrid(grid: number[][]) {
    const columnWidths = grid[0].map((_, colIndex) =>
        Math.max(...grid.map(row => String(row[colIndex]).length)
    ));
 
    grid.forEach(row => {
        let rowStr = '';
        row.forEach((cell, colIndex) => {
            const formattedCell = String(cell).padStart(columnWidths[colIndex]);
            rowStr += formattedCell + '  '; 
        });
     
        console.log(rowStr);
    });
}

function generateRandomInteger(min: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}

function generateRandomGrid(size: number) {
    let grid = Array(size).fill(0).map(()=>new Array(size).fill(0));
    
    for (let i = 0; i < size; i++) {
        for (let j = 0; j < size; j++) {
            grid[i][j] = generateRandomInteger(1, 100);
        }
    }
    return grid;
}

function FindMaxProduct(grid: number[][], size: number) {
        let max = 0, product = 0;
        let n1 = 0, n2 = 0, n3 = 0, n4 = 0;
           
        for (let i: number = 0; i < size; i++) {
            for (let j: number = 0; j < size - 3; j++) {
                product = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3];
                if (product > max) {
                    n1 = grid[i][j]; n2 = grid[i][j + 1]; n3 = grid[i][j + 2]; n4 = grid[i][j + 3];
                    max = product;
                }
            }
        }
                      
        for (let i: number = 0; i < size; i++) {
            for (let j: number = 0; j < size - 3; j++) {
                product = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i];
                if (product > max) {
                    n1 = grid[i][j]; n2 = grid[j + 1][i]; n3 = grid[j + 2][i]; n4 = grid[j + 2][i];
                    max = product;
                }
            }
        }
              
        for (let i: number = 0; i < size - 3; i++) {
            for (let j: number = 0; j < size - 3; j++) {
                product = grid[j][i] * grid[j + 1][i + 1] * grid[j + 2][i + 2] * grid[j + 3][i + 3];
                if (product > max) {
                    n1 = grid[j][i]; n2 = grid[j + 1][i + 1]; n3 = grid[j + 2][i + 2]; n4 = grid[j + 3][i + 3];
                    max = product;
                }
            }
        }
              
        for (let i: number = 0; i < size - 3; i++) {
            for (let j: number = 3; j < size; j++) {
                product = grid[j][i] * grid[j - 1][i + 1] * grid[j - 2][i + 2] * grid[j - 3][i + 3];
                if (product > max) {
                    n1 = grid[j][i]; n2 = grid[j - 1][i + 1]; n3 = grid[j - 2][i + 2]; n4 = grid[j - 3][i + 3];
                    max = product;
                }
            }
        }
                       
        console.log("\n" + n1 + " * " + n2 + " * " + n3 + " * " + n4 + " = ");
    
        return max;
    }
 

const grid = generateRandomGrid(20);
 
printAlignedGrid(grid);

console.log(FindMaxProduct(grid, 20));





/*
run:

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

*/

 



answered Nov 3, 2023 by avibootz
...