Advertisement
Todorov_Stanimir

The largest product of four adjacent numbersumbers

Oct 29th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(str) {
  2.     let matrix = str.split(', ').map(Number);
  3.     let newMatrix = []
  4.     let result = [Number.MIN_SAFE_INTEGER];
  5.     for (let i = 0; i < 30; i++) {
  6.         let row = matrix.splice(0, 30);
  7.         newMatrix.push(row)
  8.     }
  9.  
  10.     for (let row = 0; row < 30; row++) {
  11.         for (let col = 0; col < 30; col++) {
  12.             if (col <= 26) {
  13.                 result = calculateByRow(row, col, result);
  14.             }
  15.             if (row <= 26) {
  16.                 result = calculateByCol(row, col, result);
  17.             }
  18.  
  19.             if (col <= 26 && row <= 26) {
  20.                 result = calculateByRightDiag(row, col, result);
  21.             }
  22.             if (col >= 3 && row <= 26) {
  23.                 result = calculateByLeftDiag(row, col, result);
  24.             }
  25.         }
  26.     }
  27.  
  28.     function calculateByCol(row, col, result) {
  29.         let number = 1;
  30.         let numberInString = ''
  31.         for (let r = row; r < row + 4; r++) {
  32.             number *= newMatrix[r][col];
  33.             numberInString += String(newMatrix[r][col]) + ' ';
  34.         }
  35.         return (result[0] > number) ? [result[0], result[1]] : [number, numberInString.trim()];
  36.     }
  37.     function calculateByRow(row, col, result) {
  38.         let number = 1;
  39.         let numberInString = ''
  40.         for (let c = col; c < col + 4; c++) {
  41.             number *= newMatrix[row][c]
  42.             numberInString += String(newMatrix[row][c]) + ' ';
  43.         }
  44.         return (result[0] > number) ? [result[0], result[1]] : [number, numberInString.trim()];
  45.     }
  46.  
  47.     function calculateByRightDiag(row, col, result) {
  48.         let number = 1;
  49.         let numberInString = ''
  50.         for (let i = 0; i < 4; i++) {
  51.             number *= newMatrix[row + i][col + i];
  52.             numberInString += String(newMatrix[row + i][col + i]) + ' ';
  53.         }
  54.         return (result[0] > number) ? [result[0], result[1]] : [number, numberInString.trim()];
  55.     }
  56.  
  57.     function calculateByLeftDiag(row, col, result) {
  58.         let number = 1;
  59.         let numberInString = ''
  60.         for (let i = 0; i < 4; i++) {
  61.             number *= newMatrix[row + i][col - i];
  62.             numberInString += String(newMatrix[row + i][col - i]) + ' ';
  63.         }
  64.         return (result[0] > number) ? [result[0], result[1]] : [number, numberInString.trim()];
  65.     }
  66.  
  67.     return result[0]
  68. }
  69.  
  70. console.log(solve('9, 14, 8, 11, 16, 30, 79, 78, 7, 87, 26, 18, 81, 82, 45, 28, 17, 17, 83, 27, 66, 2, 69, 79, 96, 34, 71, 69, 56, 80, 10, 1, 45, 60, 98, 80, 82, 42, 83, 6, 71, 19, 68, 63, 23, 94, 42, 24, 90, 10, 81, 19, 79, 91, 96, 16, 85, 38, 47, 61, 32, 78, 84, 94, 66, 80, 95, 31, 97, 65, 59, 33, 1, 87, 23, 35, 71, 26, 3, 24, 21, 89, 43, 8, 75, 40, 38, 88, 69, 32, 14, 11, 15, 30, 84, 88, 42, 48, 65, 47, 78, 70, 81, 64, 74, 73, 46, 50, 4, 10, 28, 15, 90, 77, 65, 50, 49, 78, 60, 70, 16, 29, 48, 37, 53, 19, 23, 53, 14, 81, 19, 35, 96, 17, 50, 75, 45, 10, 71, 56, 2, 31, 92, 83, 45, 91, 59, 76, 81, 45, 99, 55, 88, 8, 59, 11, 31, 28, 10, 91, 62, 26, 95, 75, 74, 97, 38, 14, 36, 40, 68, 93, 66, 40, 68, 30, 84, 97, 74, 95, 9, 48, 26, 3, 53, 38, 31, 85, 76, 29, 5, 51, 89, 90, 96, 13, 7, 91, 36, 33, 7, 58, 56, 84, 40, 94, 90, 37, 68, 25, 61, 28, 51, 75, 52, 31, 36, 9, 37, 51, 53, 45, 33, 59, 43, 57, 20, 61, 54, 67, 42, 21, 23, 60, 39, 18, 8, 73, 53, 96, 69, 77, 12, 77, 67, 7, 98, 94, 99, 96, 98, 30, 97, 16, 48, 92, 21, 98, 18, 82, 88, 4, 39, 9, 66, 62, 3, 78, 58, 84, 9, 35, 40, 91, 82, 5, 88, 90, 33, 1, 94, 47, 28, 73, 89, 90, 20, 69, 52, 12, 85, 64, 27, 97, 84, 76, 68, 31, 55, 29, 78, 45, 16, 87, 41, 29, 49, 23, 18, 31, 56, 42, 98, 76, 64, 94, 84, 98, 9, 22, 33, 50, 16, 85, 90, 9, 24, 94, 31, 78, 41, 20, 66, 3, 6, 38, 9, 94, 80, 80, 5, 50, 75, 51, 58, 43, 47, 79, 48, 39, 90, 19, 24, 37, 70, 49, 50, 17, 18, 16, 80, 77, 4, 9, 30, 46, 57, 94, 73, 27, 38, 29, 59, 61, 22, 86, 33, 98, 90, 85, 60, 54, 70, 53, 78, 23, 45, 23, 71, 18, 21, 50, 70, 53, 21, 5, 4, 75, 2, 17, 66, 99, 6, 76, 83, 31, 71, 82, 18, 39, 70, 20, 61, 42, 39, 67, 77, 8, 21, 71, 76, 98, 81, 54, 25, 43, 49, 25, 9, 66, 71, 43, 83, 54, 93, 21, 86, 69, 20, 9, 1, 52, 54, 28, 99, 32, 37, 92, 89, 49, 71, 85, 45, 76, 48, 79, 54, 35, 21, 74, 15, 20, 49, 8, 20, 24, 57, 69, 78, 1, 13, 84, 66, 94, 15, 54, 52, 27, 75, 75, 59, 84, 53, 16, 70, 52, 14, 6, 44, 34, 61, 28, 34, 93, 21, 87, 26, 58, 62, 17, 46, 38, 82, 63, 43, 69, 63, 64, 88, 93, 95, 79, 77, 85, 96, 99, 6, 8, 43, 29, 91, 11, 81, 52, 11, 76, 33, 89, 71, 76, 33, 56, 44, 74, 86, 71, 56, 38, 43, 72, 44, 38, 21, 28, 19, 44, 9, 26, 71, 3, 75, 42, 44, 43, 77, 38, 84, 3, 18, 4, 87, 16, 3, 74, 51, 68, 61, 62, 22, 56, 67, 63, 14, 22, 38, 8, 57, 54, 29, 90, 16, 54, 1, 74, 67, 29, 40, 67, 40, 97, 60, 3, 11, 94, 62, 45, 19, 37, 75, 61, 6, 20, 26, 90, 78, 26, 68, 36, 65, 41, 74, 77, 75, 25, 70, 2, 47, 91, 48, 87, 14, 83, 90, 42, 35, 21, 52, 21, 58, 81, 47, 14, 10, 94, 34, 12, 62, 82, 77, 5, 67, 67, 73, 73, 38, 16, 57, 74, 78, 63, 19, 89, 96, 64, 20, 88, 66, 76, 74, 88, 73, 56, 76, 39, 73, 62, 24, 76, 21, 72, 3, 57, 59, 66, 19, 12, 98, 46, 83, 95, 96, 44, 59, 96, 58, 99, 23, 43, 22, 57, 98, 58, 12, 30, 83, 88, 48, 47, 2, 6, 59, 18, 92, 56, 20, 94, 82, 71, 87, 80, 90, 93, 80, 38, 85, 10, 18, 13, 61, 1, 10, 52, 74, 39, 56, 60, 38, 14, 39, 18, 76, 9, 81, 30, 51, 98, 98, 90, 46, 22, 97, 4, 69, 75, 1, 2, 4, 44, 46, 40, 48, 41, 44, 65, 71, 24, 66, 18, 55, 73, 69, 22, 5, 83, 45, 76, 83, 6, 92, 8, 38, 52, 54, 56, 47, 71, 34, 4, 64, 63, 99, 87, 42, 20, 34, 35, 82, 50, 12, 89, 26, 52, 2, 13, 93, 3, 71, 78, 32, 73, 47, 9, 52, 8, 66, 70, 41, 89, 77, 67, 94, 67, 49, 73, 11, 23, 41, 45, 14, 40, 51, 48, 30, 3, 71, 14, 63, 97, 71, 8, 48, 8, 15, 85, 57, 92, 56, 21, 58, 75, 18, 64, 22, 73, 37, 73, 50, 55, 91, 35, 39, 89, 66, 71, 18, 34, 17, 82, 75, 97, 24, 44, 43, 80, 58, 60, 89, 35, 96, 77, 95, 37, 80, 82, 77, 53, 55, 92, 82, 51, 27, 26, 80, 19, 4, 24, 9, 92, 55, 60, 90, 75, 75, 75, 43, 89, 73, 85, 14, 61'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement