Last active
December 11, 2017 23:02
-
-
Save JoshCheek/ac5dc3be95e73e5cd29de865d15d492a to your computer and use it in GitHub Desktop.
Rule 30 in JavaScript
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
// you can also paste it into here: http://hello.processing.org/editor/ | |
// (but use their `rect` function, not the one below) | |
function rect(x,y,w,h) { | |
//console.log("rect("+x+", "+y+", "+w+", "+h+");") | |
x /= 3 | |
y /= 6 | |
x = parseInt(x) | |
y = parseInt(y) | |
process.stdout.write("\033["+(y+1)+";"+(x+1)+"H\033[41m \033[0m"); | |
} | |
var i, j, side=6, array=[], rule=30, bits=[]; | |
for(i = 0; i < 160; ++i) array.push(0); | |
array[79] = 1; | |
for(i = 0; i < 8; ++i) bits[i] = 1&(rule>>i); | |
for(i = 0; i < 40; ++i) { | |
var crntRow = array.slice(39-i, 39-i+80); | |
for(j = 0; j < crntRow.length; ++j) | |
if(crntRow[j]) { | |
rect(side*j+1, side*i+1, side, side); | |
rect(side*j, side*i, side, side); | |
} | |
var nextArray = []; | |
for(j = 0; j < array.length-2; ++j) | |
nextArray[j] = bits[array[j]*4 + array[j+1]*2 + array[j+2]]; | |
array = nextArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment