@@ -2,81 +2,35 @@ import { logger as console } from '../logger';
2
2
import { maximum } from './helpers' ;
3
3
4
4
function problem0011 ( _squareMatrix : number [ ] [ ] , _interval : number ) : number {
5
- const top = _squareMatrix . length ;
6
-
7
5
let max = 0 ;
8
- let acum ;
9
-
10
- for ( let i = 0 ; i < top ; i ++ ) {
11
- for ( let j = 0 ; j < top ; j ++ ) {
12
- acum = 1 ;
13
6
14
- if ( i < top - ( _interval - 1 ) ) {
7
+ const quadrantSize = _interval ;
8
+ const matrixLimit = _squareMatrix . length - ( _interval - 1 ) ;
9
+
10
+ for ( let i = 0 ; i < matrixLimit ; i ++ ) {
11
+ for ( let j = 0 ; j < matrixLimit ; j ++ ) {
12
+ let verticalAcum = 1 ;
13
+ let horizontalAcum = 1 ;
14
+ let diag1Acum = 1 ;
15
+ let diag2Acum = 1 ;
16
+ console . debug ( `start point: i ${ i } , j: ${ j } ` ) ;
17
+ for ( let k = 0 ; k < quadrantSize ; k ++ ) {
18
+ console . debug ( `vertical coordinate: (i, j) = (${ i + k } , ${ j } )` ) ;
19
+ console . debug ( `horizontal coordinate: (i, j) = ($i}, ${ j + k } )` ) ;
20
+ console . debug ( `diag1 coordinate: (i, j) = (${ i + k } , ${ j + k } )` ) ;
15
21
console . debug (
16
- `---- VERTICAL ------------------------------------------ `
22
+ `diag2 coordinate: (i, j) = ( ${ i + k } , ${ j + ( quadrantSize - 1 ) - k } ) `
17
23
) ;
18
- // vertical
19
- for ( let k = 0 ; k < _interval ; k ++ ) {
20
- console . debug (
21
- `row: i ${ i + k } , column: ${ j } , _interval ${ k } => ${
22
- _squareMatrix [ i + k ] [ j ]
23
- } `
24
- ) ;
25
-
26
- acum *= _squareMatrix [ i + k ] [ j ] ;
27
- }
28
-
29
- max = maximum ( acum , max ) ;
30
- }
31
24
32
- acum = 1 ;
33
- if ( j < top - ( _interval - 1 ) ) {
34
- console . debug (
35
- `---- HORIZONTAL ----------------------------------------`
36
- ) ;
37
- // horizontal
38
- for ( let k = 0 ; k < _interval ; k ++ ) {
39
- console . debug (
40
- `row: i ${ i } , column: ${ j + k } => ${ _squareMatrix [ i ] [ j + k ] } `
41
- ) ;
42
- acum *= _squareMatrix [ i ] [ j + k ] ;
43
- }
44
-
45
- max = maximum ( acum , max ) ;
46
- }
47
-
48
- acum = 1 ;
49
- if ( i + ( _interval - 1 ) < top && j + ( _interval - 1 ) < top ) {
50
- // diagonal top left -> bottom right
51
- console . debug (
52
- `---- DIAG \\ ---------------------------------------------`
53
- ) ;
54
- for ( let k = 0 ; k < _interval ; k ++ ) {
55
- console . debug (
56
- `diag: (${ i + k } , ${ j + k } ) => ${ _squareMatrix [ i + k ] [ j + k ] } `
57
- ) ;
58
- acum *= _squareMatrix [ i + k ] [ j + k ] ;
59
- }
60
-
61
- max = maximum ( acum , max ) ;
62
- }
63
-
64
- acum = 1 ;
65
- if ( i + ( _interval - 1 ) < top && j + ( _interval - 1 ) < top ) {
66
- // diagonal top rigth -> bottom left
67
- console . debug (
68
- `---- DIAG / ---------------------------------------------`
69
- ) ;
70
- for ( let k = 0 ; k < _interval ; k ++ ) {
71
- console . debug (
72
- `diag: (${ i + k } , ${ j + ( _interval - 1 ) - k } ) => ${
73
- _squareMatrix [ i + k ] [ j + ( _interval - 1 ) - k ]
74
- } `
75
- ) ;
76
- acum *= _squareMatrix [ i + k ] [ j + ( _interval - 1 ) - k ] ;
77
- }
25
+ verticalAcum *= _squareMatrix [ i + k ] [ j ] ;
26
+ horizontalAcum *= _squareMatrix [ i ] [ j + k ] ;
27
+ diag1Acum *= _squareMatrix [ i + k ] [ j + k ] ;
28
+ diag2Acum *= _squareMatrix [ i + k ] [ j + ( quadrantSize - 1 ) - k ] ;
78
29
79
- max = maximum ( acum , max ) ;
30
+ max = maximum ( verticalAcum , max ) ;
31
+ max = maximum ( horizontalAcum , max ) ;
32
+ max = maximum ( diag1Acum , max ) ;
33
+ max = maximum ( diag2Acum , max ) ;
80
34
}
81
35
}
82
36
}
0 commit comments