Skip to content

Instantly share code, notes, and snippets.

@stoffie
Created November 24, 2015 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoffie/4a7127e5ed83a1a61e70 to your computer and use it in GitHub Desktop.
Save stoffie/4a7127e5ed83a1a61e70 to your computer and use it in GitHub Desktop.
# http://codegolf.stackexchange.com/questions/64650/generate-toothpick-sequence
n = ARGV[0].to_i
$t = (n * 2).times.map { [' '] * n * 2}
def add_backslash_top (n,m,c)
if $t[n] && $t[n][m] == " " && c!=0
$t[n][m] = '\\'
add_slash_left(n,m-1,c-1)
add_slash_right(n-1,m,c-1)
end
end
def add_backslash_bottom (n,m,c)
if $t[n] && $t[n][m] == " " && c!=0
$t[n][m] = '\\'
add_slash_right(n,m+1,c-1)
add_slash_left(n+1,m,c-1)
end
end
def add_slash_left (n,m,c)
if $t[n] && $t[n][m] == " " && c!=0
$t[n][m] = '/'
add_backslash_top(n,m-1,c-1)
add_backslash_bottom(n+1,m,c-1)
end
end
def add_slash_right (n,m,c)
if $t[n] && $t[n][m] == " " && c!=0
$t[n][m] = '/'
add_backslash_top(n-1,m,c-1)
add_backslash_bottom(n,m+1,c-1)
end
end
add_backslash_top(n-1,n-1,n)
add_backslash_bottom(n,n,n)
p $t
puts $t.map { |e| e.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment