Created
December 4, 2018 18:37
-
-
Save JoshCheek/3ee6c9472c7ab13680a5c0a6a1bca5c3 to your computer and use it in GitHub Desktop.
Rule 65
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
# https://twitter.com/clarkkampfe/status/1069960462865981440 | |
ruby -e h,w=$LINES,$COLUMNS/2' | |
rows = [{}] | |
[[6, true], [3, false], [1, true], [2, false], [1, true], [1, false]].flat_map { |n, val| [val]*n }.cycle.take(w+h).each_with_index { |val, i| rows[0][i] = val } | |
[[1, true], [3, false]].flat_map { |n, val| [val]*n }.cycle.take(h).each_with_index { |val, i| rows[0][i-w/2] = val } | |
def rows.rule | |
65 | |
end | |
def rows.at(x, y) | |
row = (self[y] ||= {}) | |
return row[x] if row.key? x | |
row[x] = y.zero? ? false : calculate( | |
at(x-1, y-1), | |
at(x , y-1), | |
at(x+1, y-1), | |
) | |
end | |
def rows.calculate(a, b, c) | |
nc = (c ? 1 : 0) << 0 | |
nb = (b ? 1 : 0) << 1 | |
na = (a ? 1 : 0) << 2 | |
1 == rule[nc + nb + na] | |
end | |
h.times do |y| | |
print w.times.map { |x| rows.at(x, y) ? "\e[47m " : "\e[40m " }.join | |
print "\e[0m" | |
end | |
gets' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment