{"name":"rule30.nu","source":"// rule30.nu — Wolfram's Rule 30 cellular automaton in NURL\n//\n// From a single live cell at the top, Rule 30 produces aperiodic\n// chaotic patterns that never settle into a cycle. Stephen Wolfram\n// famously used it as Mathematica's built-in RNG for years, and the\n// same rule turns up in the pigmentation of the textile-cone snail\n// Conus textile. Simple rule → endless complexity.\n//\n// The rule: for each cell,  new = left XOR (center OR right)\n// (bit pattern 00011110₂ = 30 over the 8 possible LCR triples.)\n//\n// NURL features on display:\n//   - Heap allocation: `malloc` + pointer cast `# *i`\n//   - Pointer subscripting `. ptr idx`\n//   - Bitwise `&` / `|` on i64 (dispatched by operand type)\n//   - Inline ternary `? cond then else` inside expressions\n//   - Nested prefix arithmetic without parens\n\n// a XOR b  =  (a | b) - (a & b)   when a,b ∈ {0,1}\n@ xor i a i b → i { ^ - | a b & a b }\n\n@ render * i row i w → v {\n    : ~ i x 0\n    ~ < x w {\n        ? == . row x 1\n        ( nurl_print `█` )\n        ( nurl_print ` ` )\n        = x + x 1\n    }\n    ( nurl_print `\\n` )\n}\n\n@ step * i cur * i nxt i w → v {\n    : ~ i j 0\n    ~ < j w {\n        // Toroidal edges: index -1 wraps to w-1, index w wraps to 0.\n        : i L . cur ? > j 0 - j 1 - w 1\n        : i C . cur j\n        : i R . cur ? < j - w 1 + j 1 0\n        = . nxt j ( xor L | C R )\n        = j + j 1\n    }\n}\n\n@ copy * i src * i dst i w → v {\n    : ~ i k 0\n    ~ < k w {\n        = . dst k . src k\n        = k + k 1\n    }\n}\n\n@ main → i {\n    : i width 79\n    : i gens 40\n\n    : *i cur # *i ( malloc * width 8 )\n    : *i nxt # *i ( malloc * width 8 )\n\n    // Zero both buffers, then plant a single live cell in the centre.\n    : ~ i i 0\n    ~ < i width {\n        = . cur i 0\n        = . nxt i 0\n        = i + i 1\n    }\n    = . cur / width 2 1\n\n    ( nurl_print `\\nRule 30 — Wolfram's chaotic cellular automaton\\n\\n` )\n\n    : ~ i g 0\n    ~ < g gens {\n        ( render cur width )\n        ( step cur nxt width )\n        ( copy nxt cur width )\n        = g + g 1\n    }\n\n    ( nurl_print `\\nFrom a single seed cell: pure chaos — yet 100% deterministic.\\n` )\n    ^ 0\n}\n","bytes":2155}