Wolfram Science Summer School and Wolfram Innovation Summer School are happening now in Boston at Bentley University. The goal of this Code Jam is to post interesting code snippets that fit the Wolfram Language functionality described in detail below. By "interesting" I mean simple programs that generate complex patterns. We will be jamming with our students, but everyone is welcome to join. Let's have some fun!
In this Code Jam we will take a look at the CellularAutomaton (CA) function and its syntax for rules defined as functions. This may sound a bit confusing, so let's take a look at some examples. First, if you haven't yet, you have to make yourself familiar with the CellularAutomaton function. The most well known cases are integer Wolfram indexes for CA rules, for instance for the celebrated rule 30:
ArrayPlot[CellularAutomaton[30, RandomInteger[1, 100], 50]]
But CellularAutomaton can take a function as a rule. Below are some examples. Use an arbitrary symbolic function f as the rule to apply to range-1 neighbors:
CellularAutomaton[{f[#] &, {}, 1}, {a, b, c}, 1]
{{a, b, c}, {f[{c, a, b}], f[{a, b, c}], f[{b, c, a}]}}
Set up a "Pascal's triangle cellular automaton":
CellularAutomaton[{f[#] &, {}, 1/2}, {a, b, c}, 1]
{{a, b, c}, {f[{c, a}], f[{a, b}], f[{b, c}]}}
CellularAutomaton[{Total[#] &, {}, 1/2}, {{1}, 0}, 3]
{{1, 0, 0, 0}, {1, 1, 0, 0}, {1, 2, 1, 0}, {1, 3, 3, 1}}
Additive cellular automaton modulo 4:
ArrayPlot[
CellularAutomaton[{Mod[Total[#], 4] &, {}, 1}, {{1}, 0}, 50],
ColorFunction -> "Rainbow"]
The second argument to the function is the step number:
CellularAutomaton[{f[#1, #2] &, {}, 1}, {a, b, c}, 1]
{{a, b, c}, {f[{c, a, b}, 1], f[{a, b, c}, 1], f[{b, c, a}, 1]}}
CellularAutomaton[{f, {}, 1}, {a, b, c}, 1]
{{a, b, c}, {f[{c, a, b}, 1], f[{a, b, c}, 1], f[{b, c, a}, 1]}}
Change the rule at successive steps; #2 gives the step number:
ArrayPlot[
CellularAutomaton[{Mod[Total[#] + #2, 4] &, {}, 1}, {{1}, 0}, 30],
ColorFunction -> "Rainbow"]
Use continuous values for cells:
ArrayPlot[CellularAutomaton[{Mod[Total[#]/2, 1] &, {}, 1}, {{1}, 0}, 50]]
Manipulate[
ArrayPlot[
CellularAutomaton[{Mod[s Total[#], 1] &, {}, 2}, {{1}, 0}, 50],
ColorFunction -> "Rainbow", PixelConstrained -> 3]
, {s, .01, .99}]
Try different functions and different initial conditions. They can be strings, numbers, graphics or expressions. Try ArrayPlot but also try other visualization tools like Grid. Try them out. Post code, images, and text comments. You can also comment on other people's code.