Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,42 @@
|
|||
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(i - 1) == '#' ? 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
public class Life{
|
||||
private static char[] trans = "___#_##_".toCharArray();
|
||||
|
||||
private static int v(StringBuilder cell, int i){
|
||||
return (cell.charAt(i) != '_') ? 1 : 0;
|
||||
}
|
||||
|
||||
public static boolean evolve(StringBuilder cell){
|
||||
boolean diff = false;
|
||||
StringBuilder backup = new StringBuilder(cell.toString());
|
||||
|
||||
for(int i = 1; i < cell.length() - 3; i++){
|
||||
/* use left, self, right as binary number bits for table index */
|
||||
backup.setCharAt(i, trans[v(cell, i - 1) * 4 + v(cell, i) * 2
|
||||
+ v(cell, i + 1)]);
|
||||
diff = diff || (backup.charAt(i) != cell.charAt(i));
|
||||
}
|
||||
|
||||
cell.delete(0, cell.length());//clear the buffer
|
||||
cell.append(backup);//replace it with the new generation
|
||||
return diff;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
StringBuilder c = new StringBuilder("_###_##_#_#_#_#__#__\n");
|
||||
|
||||
do{
|
||||
System.out.printf(c.substring(1));
|
||||
}while(evolve(c));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue