Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created August 2, 2016 17:47
Show Gist options
  • Save JoshCheek/c2e999f3b306398f5bb6935ce9ac8b6b to your computer and use it in GitHub Desktop.
Save JoshCheek/c2e999f3b306398f5bb6935ce9ac8b6b to your computer and use it in GitHub Desktop.
Terminal L-System in a tweet
Posted here https://twitter.com/josh_cheek/status/760519587758690304
Previously https://twitter.com/josh_cheek/status/667501443226558464
Based on http://algorithmicbotany.org/papers/abop/abop-ch1.pdf
ruby -e 's = "F-F-F-F"; 3.times { s = s.gsub /f/i, "F" => "FF-F-F-F-FF" };
dirs = [" \e[2D\e[A", " ", " \e[2D\e[B", " \e[4D"].map { |s| s * 2 }
print "\e[H\e[2J\e[60;20H\e[45m" # clear and "center"
s.each_char { |c| c == "F" ? print("\e[45m",dirs[0]) : c == "f" ? print("\e[49m", dirs[0]) : c == "-" ? dirs.rotate!(1) : c == "+" ? dirs.rotate!(-1) : :noop }
puts'
ruby -e 'd=%w[A 2C B 2D].map { |s| " \e[2D\e[#{s}" * 2 }
print "\e[H\e[2J\e[60;20H\e[45m"
s = "F-F-F-F"; 3.times {s.gsub! /f/i, "F" => "FF-F-F-F-FF" }
s.each_char { |c| c == "F" ? print("\e[45m",d[0]) : c == "f" ? print("\e[49m", d[0]) : c == "-" ? d.rotate!(1) : c == "+" ? d.rotate!(-1) : :noop }
puts'
ruby -e 'd=%w[2C B 2D A].map{|s|" \e[2D\e[#{s}"*2}
$><<"\e[H\e[2J\e[45m"
3.times.reduce("RDLU"){|s|s.gsub /./, "R"=>"RRDLURR", "D"=>"DDLURDD", "U"=>"UURDLUU", "L"=>"LLURDLL"}.chars{|c|$><<(c=="R"?d[0]:c=="D"?d[1]:c=="L"?d[2]:c=="U"?d[3]:"")};$><<"\e[57H"'
ruby -e 'd=%w[2C B 2D A];$><<"\e[H\e[2J\e[45m"
3.times.reduce("CBDA"){|s|s.gsub /./, "C"=>"CCBDACC", "B"=>"BBDACBB", "A"=>"AACBDAA", "D"=>"DDACBDD"}.chars{|c|$><<" \e[2D\e[#{2 if c == "C" || c == "D"}#{c}"*2};$><<"\e[57H"'
ruby -e 'd=%w[2C B 2D A];$><<"\e[H\e[2J\e[45m";3.times.reduce("F-F-F-F"){|s|s.gsub"F","FF-F-F-F-FF"}.chars{|c|c=="-"?d.rotate!(1):$><<" \e[2D\e[#{d[0]}"*2}'
ruby -e '3.times.reduce("CBDA"){|s|s.gsub /./,%w[C CCBDACC B BBDACBB A AACBDAA D DDACBDD].each_slice(2).to_h}.gsub(/(C|D)|./){$><<"\e[45m \e[2D\e[#{$1&&2}#$&"*2};$><<"\e[58H"'
ruby -e '3.times.reduce(0..3){|r|r.flat_map{|n|[0,0,1,2,3,0,0].map{|n2|n+n2}}}.map{|n|$><<"\e[45m \e[2D\e[#{2 if n.even?}#{"CBDA"[n%4]}"*2};$><<"\e[58H"'
ruby -e 'r=*0..3;3.times{r=r.flat_map{|n|[0,0,1,2,3,0,0].map{|m|n+m}}};r.map{|n|$><<"\e[45m \e[2D\e[#{2 if n.even?}#{"CBDA"[n%4]}"*2};$><<"\e[58H"'
ruby -e 'r=*0..3;3.times{r=r.flat_map{|n|[0,0,1,2,3,0,0].map{|m|n+m}}};r.map{|n|$><<"\e[45m \e[2D\e[#{2*(~n)[0]}#{"CBDA"[n%4]}"*2};$><<"\e[58H"'
ruby -e 'r=1..4;3.times{r=r.flat_map{|n|[0,0,1,2,3,0,0].map{|m|n+m}}};r.map{|n|$><<"\e[45m \e[2D\e[#{2*n[0]}#{"ACBD"[n%4]}"*2};$><<"\e[58H"'
ruby -e 'r=1..4;3.times{r=r.flat_map{|n|[0,0,1,2,3,0,0].map{|m|n+m}}};$><<r.map{|n|"\e[45m \e[2D\e[#{n%2*2}#{"ACBD"[n%4]}"*2}*""<<"\e[58H"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment