Created
October 21, 2008 12:49
-
-
Save code/18289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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