Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active December 9, 2017 03:29
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 JoshCheek/9f84c3db437d4149ad060bc49e0f28eb to your computer and use it in GitHub Desktop.
Save JoshCheek/9f84c3db437d4149ad060bc49e0f28eb to your computer and use it in GitHub Desktop.
ruby -e '
require "io/console"
print "\e[?25l"
at_exit { print "\e[H\e[2J\e[?25h" }
seed = 0
loop do
rule = Random.new(seed).rand(2**64)
nrows, ncols = $stdin.winsize
rows = [Array.new(nrows*2+ncols/2+2, 0), Array.new(nrows*2+ncols/2, 0)]
rows.each { |r| rand(5).times { r[rand r.size] = 1 } }
to_print = ""
nrows.times do |i|
cells1, cells2 = rows
cells = cells2[nrows-i, ncols/2]
slices = cells.zip([0]+cells).slice_before { |l, r| l != r }.map { |row| row.map &:first }
to_print << "\e[#{i+1}H"
slices.each { |slice| to_print << "\e[4#{slice[0]}m" << (" "*slice.size) }
rows = [
cells2,
cells1[1...-1].zip(cells2).each_cons(3).map { |(a,d),(b,e),(c,f)| rule[a*32+b*16+c*8+d*4+e*2+f] },
]
end
to_print << "\e[H\e[97;44mseed #{seed} | rule #{rule}\e[49;34m (press j/k/q)\e[0m"
$stdout.write to_print
$stdout.flush
input = $stdin.getch
next seed += 1 if input == ?k
next seed -= 1 if input == ?j
break if %W[\C-c \C-d q \e].include? input
end'
ruby -e '
require "io/console"
print "\e[?25l"
at_exit { print "\e[H\e[2J\e[?25h" }
seed = 0
loop do
rule = Random.new(seed).rand(2**64)
nrows, ncols = $stdin.winsize
rows = [Array.new(nrows*2+ncols/2+2, 0), Array.new(nrows*2+ncols/2, 0),]
rows[-1][rows[-1].size/2] = 1
to_print = $stdout
nrows.times do |i|
cells1, cells2 = rows
cells = cells2[nrows-i, ncols/2]
slices = cells.zip([0]+cells).slice_before { |l, r| l != r }.map { |row| row.map &:first }
to_print << "\e[#{i+1}H"
slices.each { |slice| to_print << "\e[4#{slice[0]}m" << (" "*slice.size) }
rows = [
cells2,
cells1[1...-1].zip(cells2).each_cons(3).map { |(a,d),(b,e),(c,f)| rule[a*32+b*16+c*8+d*4+e*2+f] },
]
end
to_print << "\e[H\e[97;44mseed #{seed} | rule #{rule}\e[49;34m (press j/k/q)\e[0m"
#$stdout.write to_print
$stdout.flush
input = $stdin.getch
next seed += 1 if input == ?k
next seed -= 1 if input == ?j
break if %W[\C-c \C-d q \e].include? input
end'
# based on looking at 5 instead of 3
ruby -rio/console -e '
rule = (ARGV[0]||raise("PROVIDE THE RULE AS AN ARGUMENT!")).to_i
loop do
rows, cols = $stdin.winsize
vals = Array.new rows*4 + cols/2, 0
vals[vals.size/2-1] = 1
rows.times do |i|
print "\e[#{i+1}H", vals[rows*2-i*2, cols/2].map { |v| "\e[4#{v}m \e[0" }.join
vals = vals.each_cons(5).tap { |v| }.map { |a,b,c,d,e| rule[a*16+b*8+c*4+d*2+e*1] }
end
print "\e[H\e[47;30mRULE #{rule}\e[0m"
input = $stdin.getch
if input == ?j
rule += 1
elsif input == ?k
rule -= 1
elsif input == q
break
end
end
' -- 1208058119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment