Skip to content

Commit

Permalink
Merge pull request #152 from sir-gon/problem0011-refactor
Browse files Browse the repository at this point in the history
[REFACTOR] problem0011 complexity reduced.
  • Loading branch information
sir-gon authored Aug 2, 2023
2 parents 023bb12 + 42aab6d commit a4d05c4
Showing 1 changed file with 23 additions and 69 deletions.
92 changes: 23 additions & 69 deletions src/projecteuler/problem0011.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,35 @@ import { logger as console } from '../logger';
import { maximum } from './helpers';

function problem0011(_squareMatrix: number[][], _interval: number): number {
const top = _squareMatrix.length;

let max = 0;
let acum;

for (let i = 0; i < top; i++) {
for (let j = 0; j < top; j++) {
acum = 1;

if (i < top - (_interval - 1)) {
const quadrantSize = _interval;
const matrixLimit = _squareMatrix.length - (_interval - 1);

for (let i = 0; i < matrixLimit; i++) {
for (let j = 0; j < matrixLimit; j++) {
let verticalAcum = 1;
let horizontalAcum = 1;
let diag1Acum = 1;
let diag2Acum = 1;
console.debug(`start point: i ${i}, j: ${j}`);
for (let k = 0; k < quadrantSize; k++) {
console.debug(`vertical coordinate: (i, j) = (${i + k}, ${j})`);
console.debug(`horizontal coordinate: (i, j) = ($i}, ${j + k})`);
console.debug(`diag1 coordinate: (i, j) = (${i + k}, ${j + k})`);
console.debug(
`---- VERTICAL ------------------------------------------`
`diag2 coordinate: (i, j) = (${i + k}, ${j + (quadrantSize - 1) - k})`
);
// vertical
for (let k = 0; k < _interval; k++) {
console.debug(
`row: i ${i + k}, column: ${j}, _interval ${k} => ${
_squareMatrix[i + k][j]
}`
);

acum *= _squareMatrix[i + k][j];
}

max = maximum(acum, max);
}

acum = 1;
if (j < top - (_interval - 1)) {
console.debug(
`---- HORIZONTAL ----------------------------------------`
);
// horizontal
for (let k = 0; k < _interval; k++) {
console.debug(
`row: i ${i}, column: ${j + k} => ${_squareMatrix[i][j + k]}`
);
acum *= _squareMatrix[i][j + k];
}

max = maximum(acum, max);
}

acum = 1;
if (i + (_interval - 1) < top && j + (_interval - 1) < top) {
// diagonal top left -> bottom right
console.debug(
`---- DIAG \\ ---------------------------------------------`
);
for (let k = 0; k < _interval; k++) {
console.debug(
`diag: (${i + k}, ${j + k}) => ${_squareMatrix[i + k][j + k]}`
);
acum *= _squareMatrix[i + k][j + k];
}

max = maximum(acum, max);
}

acum = 1;
if (i + (_interval - 1) < top && j + (_interval - 1) < top) {
// diagonal top rigth -> bottom left
console.debug(
`---- DIAG / ---------------------------------------------`
);
for (let k = 0; k < _interval; k++) {
console.debug(
`diag: (${i + k}, ${j + (_interval - 1) - k}) => ${
_squareMatrix[i + k][j + (_interval - 1) - k]
}`
);
acum *= _squareMatrix[i + k][j + (_interval - 1) - k];
}
verticalAcum *= _squareMatrix[i + k][j];
horizontalAcum *= _squareMatrix[i][j + k];
diag1Acum *= _squareMatrix[i + k][j + k];
diag2Acum *= _squareMatrix[i + k][j + (quadrantSize - 1) - k];

max = maximum(acum, max);
max = maximum(verticalAcum, max);
max = maximum(horizontalAcum, max);
max = maximum(diag1Acum, max);
max = maximum(diag2Acum, max);
}
}
}
Expand Down

0 comments on commit a4d05c4

Please sign in to comment.