hackerrank/interview_preparation_kit_arrays.rb
# Complete the hourglassSum function below.

Complete the hourglassSum function below.
Complete the hourglassSum function below .
def hourglassSum(arr)
  rc_arr = [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]
  ]
  arr.each_with_index do |row, i|
    row.each_with_index do |cell, j|
      rc_arr[i][j] += cell if (0..3).include?(j) && (0..3).include?(i)
      rc_arr[i][j-1] += cell if (0..3).include?(j-1) && (0..3).include?(i)
      rc_arr[i][j-2] += cell if (0..3).include?(j-2) && (0..3).include?(i)
      rc_arr[i-1][j-1] += cell if (0..3).include?(j-1) && (0..3).include?(i-1)
      rc_arr[i-2][j] += cell if (0..3).include?(j) && (0..3).include?(i-2)
      rc_arr[i-2][j-1] += cell if (0..3).include?(j-1) && (0..3).include?(i-2)
      rc_arr[i-2][j-2] += cell if (0..3).include?(j-2) && (0..3).include?(i-2)
    end
  end
  rc_arr.flatten.max
end
def hourglassSum ( arr ) rc_arr = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] arr . each_with_index do | row , i | row . each_with_index do | cell , j | rc_arr [ i ] [ j ] += cell if ( 0 .. 3 ) . include? ( j ) && ( 0 .. 3 ) . include? ( i ) rc_arr [ i ] [ j - 1 ] += cell if ( 0 .. 3 ) . include? ( j - 1 ) && ( 0 .. 3 ) . include? ( i ) rc_arr [ i ] [ j - 2 ] += cell if ( 0 .. 3 ) . include? ( j - 2 ) && ( 0 .. 3 ) . include? ( i ) rc_arr [ i - 1 ] [ j - 1 ] += cell if ( 0 .. 3 ) . include? ( j - 1 ) && ( 0 .. 3 ) . include? ( i - 1 ) rc_arr [ i - 2 ] [ j ] += cell if ( 0 .. 3 ) . include? ( j ) && ( 0 .. 3 ) . include? ( i - 2 ) rc_arr [ i - 2 ] [ j - 1 ] += cell if ( 0 .. 3 ) . include? ( j - 1 ) && ( 0 .. 3 ) . include? ( i - 2 ) rc_arr [ i - 2 ] [ j - 2 ] += cell if ( 0 .. 3 ) . include? ( j - 2 ) && ( 0 .. 3 ) . include? ( i - 2 ) end end rc_arr . flatten . max end
Complete the hourglassSum function below.
Complete the hourglassSum function below .
arr
<not_specific>