@@ -177,10 +177,7 @@ function howManyTimes(words, target) {
177
177
// Iteration #8: Bonus
178
178
const matrix = [
179
179
[ 8 , 2 , 22 , 97 , 38 , 15 , 0 , 40 , 0 , 75 , 4 , 5 , 7 , 78 , 52 , 12 , 50 , 77 , 91 , 8 ] ,
180
- [
181
- 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 4 , 56 , 62 ,
182
- 0 ,
183
- ] ,
180
+ [ 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 4 , 56 , 62 , 0 ] ,
184
181
[
185
182
81 , 49 , 31 , 73 , 55 , 79 , 14 , 29 , 93 , 71 , 40 , 67 , 53 , 88 , 30 , 3 , 49 , 13 , 36 ,
186
183
65 ,
@@ -241,28 +238,62 @@ const matrix = [
241
238
] ;
242
239
243
240
function greatestProduct ( matrix ) {
244
- let oneCounter = 0 ;
245
- let twoCounter = 0 ;
241
+ let prodMax = 0 ;
242
+
243
+ let currRow = 0 ;
244
+ let currCol = 0 ;
245
+
246
+ for ( let i = 0 ; i < matrix . length ; i ++ ) {
247
+ currRow = matrix [ i ] . slice ( 0 , 4 ) . reduce ( ( prev , curr ) => prev * curr , 1 )
248
+ currCol = matrix . slice ( 0 , 4 ) . map ( row => row [ i ] ) . reduce ( ( prev , curr ) => prev * curr , 1 )
249
+
250
+ if ( currRow > prodMax ) prodMax = currRow ;
251
+ if ( currCol > prodMax ) prodMax = currCol ;
252
+
253
+ for ( let j = 4 ; j < matrix [ i ] . length ; j ++ ) {
254
+ currRow *= matrix [ i ] [ j ]
255
+ currRow /= matrix [ i ] [ j - 4 ]
256
+
257
+ currCol *= matrix [ j ] [ i ]
258
+ currCol /= matrix [ j - 4 ] [ i ]
259
+
260
+ if ( currRow > prodMax ) prodMax = currRow ;
261
+ if ( currCol > prodMax ) prodMax = currCol ;
262
+ }
263
+ }
264
+
265
+ return prodMax ;
266
+ }
267
+
268
+ function greatestProductOfDiagonals ( matrix ) {
269
+ let prodMax = 0 ;
270
+
271
+ let currFor = 0 ;
272
+ let currBack = 0 ;
246
273
247
274
for ( let i = 0 ; i < matrix . length ; i ++ ) {
248
- for ( let j = 0 ; j < matrix [ i ] . length ; j ++ ) {
249
- if ( matrix [ i ] [ j ] !== 1 && matrix [ i ] [ j ] !== 2 ) {
250
- return 0 ;
251
- }
252
-
253
- if ( matrix [ i ] [ j ] === 1 && twoCounter === 0 ) {
254
- oneCounter ++ ;
255
- } else if ( matrix [ i ] [ j ] === 2 && oneCounter === 0 ) {
256
- twoCounter ++ ;
257
- } else {
258
- return 0 ;
259
- }
275
+ currRow = matrix [ i ] . slice ( 0 , 4 ) . reduce ( ( prev , curr ) => prev * curr , 1 )
276
+ currCol = matrix . slice ( 0 , 4 ) . map ( row => row [ i ] ) . reduce ( ( prev , curr ) => prev * curr , 1 )
277
+
278
+ if ( currRow > prodMax ) prodMax = currRow ;
279
+ if ( currCol > prodMax ) prodMax = currCol ;
280
+
281
+ for ( let j = 4 ; j < matrix [ i ] . length ; j ++ ) {
282
+ currRow *= matrix [ i ] [ j ]
283
+ currRow /= matrix [ i ] [ j - 4 ]
284
+
285
+ currCol *= matrix [ j ] [ i ]
286
+ currCol /= matrix [ j - 4 ] [ i ]
287
+
288
+ if ( currRow > prodMax ) prodMax = currRow ;
289
+ if ( currCol > prodMax ) prodMax = currCol ;
260
290
}
261
291
}
262
292
263
- return oneCounter > 0 ? 1 : 16 ;
293
+ return prodMax ;
264
294
}
265
295
296
+
266
297
// The following is required to make unit tests work.
267
298
/* Environment setup. Do not modify the below code. */
268
299
if ( typeof module !== "undefined" ) {
0 commit comments