Skip to content

Instantly share code, notes, and snippets.

@code
Created October 21, 2008 12:49
Show Gist options
  • Save code/18289 to your computer and use it in GitHub Desktop.
Save code/18289 to your computer and use it in GitHub Desktop.
# File: robot.rb
# Author: Luke Hubbard
# Gist: http://gist.github.com/gists/18289
# License: MIT
# Description: Ruby version of robot to solve Google Treasure Hunt.
def robot(rows, cols)
matrix = Array.new(rows).collect{ Array.new(cols) }
x, y = rows-1, cols-1
x.downto(0) do |row|
y.downto(0) do |col|
a = (matrix[row+1][col] unless matrix[row+1].nil?) || 0
b = matrix[row][col+1] || 0
sum = a + b
matrix[row][col] = sum > 0 ? sum : 1
end
end
matrix[0][0]
end
start = Time.now
solution = robot(48, 54)
time = Time.now - start
puts "Robot solution #{solution} computed in #{time}s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment