Created
November 24, 2015 21:03
-
-
Save stoffie/4a7127e5ed83a1a61e70 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
# 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