Skip to content

Instantly share code, notes, and snippets.

@code
Created October 21, 2008 15:16
Show Gist options
  • Save code/18325 to your computer and use it in GitHub Desktop.
Save code/18325 to your computer and use it in GitHub Desktop.
# File: c_version_robot.rb
# Author: Luke Hubbard
# Gist: http://gist.github.com/gists/18325
# License: MIT
# Description: C version of the robot for Google Treasure Hunt.
# Bugs: Cant handle big grids, numbers too big.
require 'rubygems'
require 'inline'
class CVersion
inline do |builder|
builder.include "<stdio.h>"
builder.c %{
long robot(int rows, int cols) {
long matrix[rows][cols];
int x, y;
for (x = rows-1; x >= 0; x--) {
for (y = cols-1; y >= 0; y--) {
long a = (x<rows-1) ? matrix[x+1][y] : 0;
long b = (y<cols-1) ? matrix[x][y+1] : 0;
long sum = a + b;
matrix[x][y] = sum > 0 ? sum : 1;
}
}
return matrix[0][0];
}}
end
end
puts CVersion.new.robot(17, 17)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment