Elementary cellular automaton: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
(→‎{{header|UNIX Shell}}: Add implementation.)
Line 3,481: Line 3,481:
.##..###########..##.
.##..###########..##.
######.........######
######.........######
</pre>

=={{header|UNIX Shell}}==
{{trans|Common Lisp}}
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
<lang bash>function print_cells {
local chars=(. '#') cell
for cell; do
printf '%s' "${chars[$cell]}"
done
printf '\n'
}

function next_gen {
local rule=$1
shift
cells=("$@")
local -i l c r bit
for (( l=$#-1, c=0, r=1; c < $#;
l=(l+1)%$#, c+=1, r=(r+1)%$# )); do
local left=${cells[l]} current=${cells[c]} right=${cells[r]}
(( bit = 1<<(left<<2 | current<<1 | right) ))
printf '%d\n' $(( rule & bit ? 1 : 0 ))
done
}

function automaton {
local size=${1:-32} rule=${2:-90} stop=${3:-$(( size/2 ))}
local i gen cells=()
for (( i=0; i<size; ++i )); do
cells+=( $(( i == size/2 )) )
done
for (( gen=0; gen<stop; ++gen )); do
printf "%${#stop}d: " "$gen"
print_cells "${cells[@]}"
cells=($(next_gen "$rule" "${cells[@]}"))
done
}

automaton "$@"
</lang>
{{Out}}
<pre> 0: ................#...............
1: ...............#.#..............
2: ..............#...#.............
3: .............#.#.#.#............
4: ............#.......#...........
5: ...........#.#.....#.#..........
6: ..........#...#...#...#.........
7: .........#.#.#.#.#.#.#.#........
8: ........#...............#.......
9: .......#.#.............#.#......
10: ......#...#...........#...#.....
11: .....#.#.#.#.........#.#.#.#....
12: ....#.......#.......#.......#...
13: ...#.#.....#.#.....#.#.....#.#..
14: ..#...#...#...#...#...#...#...#.
15: .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
</pre>
</pre>