lib/hourglass.rb
#Pseudocode:
#find each hourglass
#calculate and store each sum in an array
#return highest value in that array (holding each hourglass sum)
find each hourglass
calculate and store each sum in an array
return highest value in that array (holding each hourglass sum)
find each hourglass calculate and store each sum in an array return highest value in that array ( holding each hourglass sum )
def hourglass(matrix)
  raise ArgumentError, "Invalid matrix" if matrix.nil?
  hg_sums = []
  row_i = 0 
  col_i = 0 
  until hg_sums.size == 16
    hg_sums << (matrix[row_i][col_i] + matrix[row_i][col_i+1] + matrix[row_i][col_i+2] +
        matrix[row_i+1][col_i+1] +
        matrix[row_i+2][col_i] + matrix[row_i+2][col_i+1] + matrix[row_i+2][col_i+2])
    col_i += 1
    if col_i == 4
      col_i = 0
      row_i += 1
    end
  end
  return hg_sums.max
end
def hourglass ( matrix ) raise ArgumentError , "Invalid matrix" if matrix . nil? hg_sums = [ ] row_i = 0 col_i = 0 until hg_sums . size == 16 hg_sums << ( matrix [ row_i ] [ col_i ] + matrix [ row_i ] [ col_i + 1 ] + matrix [ row_i ] [ col_i + 2 ] + matrix [ row_i + 1 ] [ col_i + 1 ] + matrix [ row_i + 2 ] [ col_i ] + matrix [ row_i + 2 ] [ col_i + 1 ] + matrix [ row_i + 2 ] [ col_i + 2 ] ) col_i += 1 if col_i == 4 col_i = 0 row_i += 1 end end return hg_sums . max end
Pseudocode:
find each hourglass
calculate and store each sum in an array
return highest value in that array (holding each hourglass sum)
Pseudocode : find each hourglass calculate and store each sum in an array return highest value in that array ( holding each hourglass sum )
# track row index # track column index
matrix
<not_specific>