One-dimensional cellular automata: Difference between revisions
Content added Content deleted
(jq) |
(→{{header|jq}}: simplify) |
||
Line 1,900: | Line 1,900: | ||
<lang jq># The 1-d cellular automaton: |
<lang jq># The 1-d cellular automaton: |
||
def next: |
def next: |
||
# Conveniently, jq treats null as 0 when it comes to addition |
|||
([0] + . + [0]) |
|||
# so there is no need to fiddle with the boundaries |
|||
. as $old |
|||
| reduce range( |
| reduce range(0; length) as $i |
||
([]; |
([]; |
||
($old[$i-1] + $old[$i+1]) as $s |
($old[$i-1] + $old[$i+1]) as $s |
||
| if $s == 0 then .[$i |
| if $s == 0 then .[$i] = 0 |
||
elif $s == 1 then .[$i |
elif $s == 1 then .[$i] = (if $old[$i] == 1 then 1 else 0 end) |
||
else .[$i |
else .[$i] = (if $old[$i] == 1 then 0 else 1 end) |
||
end); |
end); |
||
# pretty-print an array: |
# pretty-print an array: |