Project Euler Problem 11

Yet another problem, this is the one I’ve had the most fun on so far. I swear it is IMPOSSIBLE to make this readable! The inspiration for the solution came from the UK TV show “catchphrase”, believe it or not 🙂 I basically slam a kernel/matrix type thingy around the grid doing Roy Walker impressions until I find the highest product. Here it is anyway, but abandon hope all ye who proceed beyond this point…

def problem11  
  grid = []
  kernel_size = 4
  max_product = 0

  File.readlines("numbers.txt").each do |line|
    grid < max_product
      }


      left_diagonal_total = 1
      right_diagonal_total = 1
        
      (0..3).each {|matrix_counter|
        left_diagonal_total *= grid[row+matrix_counter][col+matrix_counter].to_i
        right_diagonal_total *= grid[row+matrix_counter][col+((kernel_size-1)-matrix_counter)].to_i  
      }  
      
      max_diagonal = [left_diagonal_total,right_diagonal_total].max
      max_product = max_diagonal if max_diagonal > max_product      
      
    end
  end

  puts max_product

end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s