-
-
Save stevekrenzel/b490564bf1c7f98e232a6c863bd066dd to your computer and use it in GitHub Desktop.
wolfram.js
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
function evolve(state, rules) { | |
return state.map((item, i) => | |
rules[(4 * (state[i - 1] || 0)) + | |
(2 * item) + | |
(1 * (state[i + 1] || 0))] | |
); | |
} | |
function parseRules(input) { | |
const rule = parseInt(input, 10); | |
const base = Array(8).fill(0); | |
return base.map((_, i) => (rule >> i) & 1); | |
} | |
function print(state) { | |
const row = state.map(item => item === 1 ? '█' : ' '); | |
console.log(row.join('')); | |
} | |
function animate(state, rules) { | |
print(state); | |
state = evolve(state, rules); | |
setTimeout(() => animate(state, rules), 100); | |
} | |
const input = process.argv[2]; | |
const rules = parseRules(input); | |
const state = new Array(process.stdout.columns).fill(0); | |
state[Math.floor(state.length / 2)] = 1; | |
animate(state, rules); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment