8 -define(DIRECTIONS, ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']).
13 -define(INTERVAL, 0). % In milliseconds
15 -define(CHAR_DEAD, $-).
16 -define(CHAR_ALIVE, $O).
17 -define(CHAR_BAR, $=).
20 main( ) -> bang(?X, ?Y).
21 main([ ]) -> bang(?X, ?Y);
22 main([X ]) -> bang(list_to_integer(X), list_to_integer(X));
23 main([X, Y]) -> bang(list_to_integer(X), list_to_integer(Y)).
26 %% ============================================================================
28 %% ============================================================================
30 %% ----------------------------------------------------------------------------
32 %% ----------------------------------------------------------------------------
36 CellIDs = lists:seq(1, N),
42 integer_to_atom(neighbor_id(D, X, ID))
45 [{integer_to_atom(ID), Neighbors} | Pairs]
54 fun({ID, Neighbors}) ->
57 spawn(fun() -> cell(ID, Parent, Neighbors) end)
60 [{ID, filter_offsides(N, Neighbors)} || {ID, Neighbors} <- Graph]
63 CellNames = [integer_to_atom(ID) || ID <- CellIDs],
68 %% ----------------------------------------------------------------------------
70 %% ----------------------------------------------------------------------------
73 ok = send_all(Cells, {tick, self()}),
77 tock(X, All, Pending, StatePairs).
80 tock(X, All, [], StatePairs) ->
83 fun({_ID, State}, States) -> [State | States] end,
85 lists:sort(StatePairs)
88 ok = do_print_states(X, States),
90 ok = timer:sleep(?INTERVAL),
93 tock(X, All, Pending, StatePairs) ->
95 {tock, {ID, State}} ->
96 NewPending = lists:delete(ID, Pending),
97 NewStatePairs = [{ID, State} | StatePairs],
98 tock(X, All, NewPending, NewStatePairs)
102 %% ----------------------------------------------------------------------------
104 %% ----------------------------------------------------------------------------
107 cell(MyID, MyParent, MyNeighbors) ->
108 MyState = crypto:rand_uniform(0, 2),
109 cell(MyID, MyParent, MyNeighbors, MyState).
112 cell(MyID, MyParent, MyNeighbors, MyState) ->
115 ok = send_all(MyNeighbors, {request_state, MyID}),
116 cell(MyID, MyParent, MyNeighbors, MyState, {MyNeighbors, []})
120 % All neighbors replied
121 cell(MyID, MyParent, MyNeighbors, MyState, {[], States}) ->
122 LiveNeighbors = lists:sum(States),
123 MyNewState = new_state(MyState, LiveNeighbors),
124 MyParent ! {tock, {MyID, MyNewState}},
125 cell(MyID, MyParent, MyNeighbors, MyNewState);
127 % Awaiting requests and replies
128 cell(MyID, MyParent, MyNeighbors, MyState, {Pending, States}) ->
130 {request_state, ID} ->
131 ID ! {response_state, MyID, MyState},
132 cell(MyID, MyParent, MyNeighbors, MyState, {Pending, States});
134 {response_state, ID, State} ->
135 NewPending = lists:delete(ID, Pending),
136 NewStates = [State | States],
137 cell(MyID, MyParent, MyNeighbors, MyState, {NewPending, NewStates})
141 %% ============================================================================
143 %% ============================================================================
145 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
146 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
147 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
148 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
149 new_state(State, _LiveNeighbors) -> State.
152 neighbor_id(Direction, X, I) ->
153 I + offset(Direction, X).
156 offset('N' , X) -> -X;
157 offset('NE', X) -> -(X - 1);
158 offset('E' , _) -> 1;
159 offset('SE', X) -> X + 1;
160 offset('S' , X) -> X;
161 offset('SW', X) -> X - 1;
162 offset('W' , _) -> -1;
163 offset('NW', X) -> -(X + 1).
166 filter_offsides(N, IDs) ->
167 [ID || ID <- IDs, is_onside(N, atom_to_integer(ID))].
170 is_onside(_, ID) when ID < 1 -> false;
171 is_onside(N, ID) when ID > N -> false;
172 is_onside(_, _) -> true.
175 %% ============================================================================
177 %% ============================================================================
179 atom_to_integer(Atom) ->
180 list_to_integer(atom_to_list(Atom)).
183 integer_to_atom(Integer) ->
184 list_to_atom(integer_to_list(Integer)).
187 send_all([], _) -> ok;
188 send_all([PID | PIDs], Msg) ->
193 do_print_states(_, []) -> ok;
194 do_print_states(X, States) ->
195 {XStates, RestStates} = lists:split(X, States),
196 ok = io:format([state_to_char(S) || S <- XStates] ++ "\n"),
197 do_print_states(X, RestStates).
201 Chars = [$+ | [?CHAR_BAR || _ <- lists:seq(1, X - 1)]],
202 io:format("~s~n", [Chars]).
205 state_to_char(0) -> ?CHAR_DEAD;
206 state_to_char(1) -> ?CHAR_ALIVE.