One-dimensional cellular automata: Difference between revisions
(Added J.) |
(Added Java) |
||
Line 29: | Line 29: | ||
__##_____#__________ |
__##_____#__________ |
||
__##________________ |
__##________________ |
||
=={{header|Java}}== |
|||
<java>public class Life{ |
|||
public static void main(String[] args) throws Exception{ |
|||
String start= "_###_##_#_#_#_#__#__"; |
|||
int numGens = 10; |
|||
for(int i= 0; i < numGens; i++){ |
|||
System.out.println("Generation " + i + ": " + start); |
|||
start= life(start); |
|||
} |
|||
} |
|||
public static String life(String lastGen){ |
|||
String newGen= ""; |
|||
for(int i= 0; i < lastGen.length(); i++){ |
|||
int neighbors= 0; |
|||
if (i == 0){//left edge |
|||
neighbors= lastGen.charAt(1) == '#' ? 1 : 0; |
|||
} else if (i == lastGen.length() - 1){//right edge |
|||
neighbors= lastGen.charAt(lastGen.length() - 2) == '#' ? 1 : 0; |
|||
} else{//middle |
|||
neighbors= getNeighbors(lastGen.substring(i - 1, i + 2)); |
|||
} |
|||
if (neighbors == 0){//dies or stays dead with no neighbors |
|||
newGen+= "_"; |
|||
} |
|||
if (neighbors == 1){//stays with one neighbor |
|||
newGen+= lastGen.charAt(i); |
|||
} |
|||
if (neighbors == 2){//flips with two neighbors |
|||
newGen+= lastGen.charAt(i) == '#' ? "_" : "#"; |
|||
} |
|||
} |
|||
return newGen; |
|||
} |
|||
public static int getNeighbors(String group){ |
|||
int ans= 0; |
|||
if (group.charAt(0) == '#') ans++; |
|||
if (group.charAt(2) == '#') ans++; |
|||
return ans; |
|||
} |
|||
}</java> |
|||
Output: |
|||
<pre>Generation 0: _###_##_#_#_#_#__#__ |
|||
Generation 1: _#_#####_#_#_#______ |
|||
Generation 2: __##___##_#_#_______ |
|||
Generation 3: __##___###_#________ |
|||
Generation 4: __##___#_##_________ |
|||
Generation 5: __##____###_________ |
|||
Generation 6: __##____#_#_________ |
|||
Generation 7: __##_____#__________ |
|||
Generation 8: __##________________ |
|||
Generation 9: __##________________</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 17:17, 9 October 2008
You are encouraged to solve this task according to the task description, using any language you may know.
One dimensional cellular automata
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values.
Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation. If, in the folowing table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
000 -> 0 # 001 -> 0 # 010 -> 0 # Dies without enough neighbours 011 -> 1 # Needs one neighbour to survive 100 -> 0 # 101 -> 1 # Two neighbours giving birth 110 -> 1 # Needs one neighbour to survive 111 -> 0 # Starved to death.
J
life1d=: '_#'{~]@(([:3&(2=+/\)0,],0:)^:a:)
Example use:
life1d ? 20 # 2 _###_##_#_#_#_#__#__ _#_#####_#_#_#______ __##___##_#_#_______ __##___###_#________ __##___#_##_________ __##____###_________ __##____#_#_________ __##_____#__________ __##________________
Java
<java>public class Life{ public static void main(String[] args) throws Exception{ String start= "_###_##_#_#_#_#__#__"; int numGens = 10; for(int i= 0; i < numGens; i++){ System.out.println("Generation " + i + ": " + start); start= life(start); } }
public static String life(String lastGen){ String newGen= ""; for(int i= 0; i < lastGen.length(); i++){ int neighbors= 0; if (i == 0){//left edge neighbors= lastGen.charAt(1) == '#' ? 1 : 0; } else if (i == lastGen.length() - 1){//right edge neighbors= lastGen.charAt(lastGen.length() - 2) == '#' ? 1 : 0; } else{//middle neighbors= getNeighbors(lastGen.substring(i - 1, i + 2)); }
if (neighbors == 0){//dies or stays dead with no neighbors newGen+= "_"; } if (neighbors == 1){//stays with one neighbor newGen+= lastGen.charAt(i); } if (neighbors == 2){//flips with two neighbors newGen+= lastGen.charAt(i) == '#' ? "_" : "#"; } } return newGen; }
public static int getNeighbors(String group){ int ans= 0; if (group.charAt(0) == '#') ans++; if (group.charAt(2) == '#') ans++; return ans; } }</java> Output:
Generation 0: _###_##_#_#_#_#__#__ Generation 1: _#_#####_#_#_#______ Generation 2: __##___##_#_#_______ Generation 3: __##___###_#________ Generation 4: __##___#_##_________ Generation 5: __##____###_________ Generation 6: __##____#_#_________ Generation 7: __##_____#__________ Generation 8: __##________________ Generation 9: __##________________
Python
<python>import random
printdead, printlive = '_#' maxgenerations = 10 cellcount = 20 offendvalue = '0'
universe = .join(random.choice('01') for i in range(cellcount))
neighbours2newstate = {
'000': '0', '001': '0', '010': '0', '011': '1', '100': '0', '101': '1', '110': '1', '111': '0', }
for i in range(maxgenerations):
print "Generation %3i: %s" % ( i, universe.replace('0', printdead).replace('1', printlive) ) universe = offendvalue + universe + offendvalue universe = .join(neighbours2newstate[universe[i:i+3]] for i in range(cellcount))
</python> Sample output:
Generation 0: _###_##_#_#_#_#__#__ Generation 1: _#_#####_#_#_#______ Generation 2: __##___##_#_#_______ Generation 3: __##___###_#________ Generation 4: __##___#_##_________ Generation 5: __##____###_________ Generation 6: __##____#_#_________ Generation 7: __##_____#__________ Generation 8: __##________________ Generation 9: __##________________