Created
September 9, 2015 23:36
-
-
Save warriordog/4263e68d9ade8e7d6536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CellularAutonoma { | |
public static void main(String[] args) { | |
boolean[] cells = parseCells(args[0]); | |
for (int age = 0; age < 25; age++) { | |
printCells(cells); | |
cells = tickCells(cells); | |
} | |
printCells(cells); | |
} | |
private static boolean[] parseCells(String state) { | |
boolean[] cells = new boolean[state.length()]; | |
for (int index = 0; index < cells.length; index++) { | |
cells[index] = state.charAt(index) == '1'; | |
} | |
return cells; | |
} | |
private static boolean[] tickCells(boolean[] cells) { | |
boolean[] nextState = new boolean[cells.length]; | |
for (int index = 0; index < cells.length; index++) { | |
nextState[index] = getState(cells, index); | |
} | |
return nextState; | |
} | |
private static boolean getState(boolean[] cells, int index) { | |
if (cells.length < 2) { | |
return false; | |
} | |
if (index == 0) { | |
return cells[1]; | |
} | |
if (index == cells.length - 1) { | |
return cells[cells.length - 2]; | |
} | |
return cells[index - 1] != cells[index + 1]; | |
} | |
private static void printCells(boolean[] cells) { | |
for (boolean bool : cells) { | |
System.out.print(bool ? 'x' : ' '); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment