Created
December 16, 2011 00:11
-
-
Save siritori/1483653 to your computer and use it in GitHub Desktop.
セルオートマトン法・ω・
This file contains hidden or 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
-module(cellular_automata). | |
-compile(export_all). | |
make_rule(N) -> | |
<<B1:1, B2:1, B3:1, B4:1, B5:1, B6:1, B7:1, B8:1>> = <<N>>, | |
fun({X1, X2, X3}) -> | |
<<Idx>> = <<0:5, X1:1, X2:1, X3:1>>, | |
lists:nth(Idx+1, [B8,B7,B6,B5,B4,B3,B2,B1]) | |
end. | |
make_field(Density, Length) -> | |
lists:map(fun(_) -> | |
case random:uniform() =< Density of | |
true -> 1; | |
false -> 0 | |
end | |
end, lists:seq(1, Length)). | |
get_part(Field, N) when N =:= length(Field) -> | |
{lists:nth(N-1, Field), lists:nth(N, Field), lists:nth(1, Field)}; | |
get_part(Field, N) when N =:= 1 -> | |
{lists:last(Field), lists:nth(N, Field), lists:nth(N+1, Field)}; | |
get_part(Field, N) -> | |
{lists:nth(N-1, Field), lists:nth(N, Field), lists:nth(N+1, Field)}. | |
apply_nth(Field, Rule, N) -> | |
io:format("~p~n", [Field]), | |
lists:foldl(fun(_, OldField) -> | |
NewField = lists:map(fun(I) -> Rule(get_part(OldField, I)) end, lists:seq(1, length(Field))), | |
io:format("~p~n", [NewField]), | |
NewField | |
end, Field, lists:seq(1, N)), | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment