Skip to content

Instantly share code, notes, and snippets.

@stevekrenzel stevekrenzel/wolfram.js Secret

Last active Jun 23, 2020
Embed
What would you like to do?
wolfram.js
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
You can’t perform that action at this time.