RosettaCodeData/Task/One-dimensional-cellular-automata/SequenceL/one-dimensional-cellular-automata.sequencel
2017-09-25 22:28:19 +02:00

30 lines
863 B
Text

import <Utilities/Conversion.sl>;
main(args(2)) :=
run(args[1], stringToInt(args[2])) when size(args) = 2
else
"Usage error: exec <initialCells> <generations>";
stringToCells(string(1))[i] := 0 when string[i] = '_' else 1;
cellsToString(cells(1))[i] := '#' when cells[i] = 1 else '_';
run(cellsString(1), generations) :=
runHelper(stringToCells(cellsString), generations, cellsString);
runHelper(cells(1), generations, result(1)) :=
let
nextCells := step(cells);
in
result when generations = 0
else
runHelper(nextCells, generations - 1,
result ++ "\n" ++ cellsToString(nextCells));
step(cells(1))[i] :=
let
left := cells[i-1] when i > 1 else 0;
right := cells[i + 1] when i < size(cells) else 0;
in
1 when (left + cells[i] + right) = 2
else
0;