Skip to content

Instantly share code, notes, and snippets.

@stevekrenzel
Last active June 23, 2020 22:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save stevekrenzel/b490564bf1c7f98e232a6c863bd066dd to your computer and use it in GitHub Desktop.
Save stevekrenzel/b490564bf1c7f98e232a6c863bd066dd to your computer and use it in GitHub Desktop.
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