• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# game_of_life_solution_01

khalfella Dec 1st, 2016 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. % Enter your code here. Read input from STDIN. Print output to STDOUT
2. % Your class should be named solution
3.
4. -module(solution).
5. -export([main/0]).
6.
7. parse_node([\$.|Txt]) -> {{leaf,\$.}, Txt};
8. parse_node([\$X|Txt]) -> {{leaf,\$X}, Txt};
9. parse_node([\$(|Txt]) ->
10.     {Left, Txt1} = parse_node(Txt),
11.     {Node, Txt2} = parse_node(Txt1),
12.     {Right, [\$)|Txt3]} = parse_node(Txt2),
13.     {{branch,Left, Node, Right}, Txt3}.
14.
15. parse_tree(Txt) ->
16.     {Tree, []} = parse_node(Txt),
17.     Tree.
18.
19. input_tree()->
20.     Txt0 = io:get_line(""),
21.     Txt = lists:filter(fun(E) -> ((E =/= 10) and (E =/= 13) and (E =/= 32)) end, Txt0),
22. %    Txt = string:strip(string:strip(Txt0, both, 13), both, 10),
23.     parse_tree(Txt).
24.
25. rules_results(_, 16, Acc) -> Acc;
26. rules_results(X, N, Acc) ->
27.     NAcc = case X rem 2 of
28.         1 -> Acc ++ [\$X];
29.         0 -> Acc ++ [\$.]
30.     end,
31.     rules_results(X div 2, N + 1, NAcc).
32.
33. val(XD) -> case XD =:= \$X of true -> 1; false -> 0 end.
34. evolve(P, L, N, R, Rules) ->
35.     Idx = val(P) * 8 + val(L) *4 + val(N) * 2 + val(R),
36.     lists:nth(Idx + 1, Rules).
37.
38.
39. node_state({leaf, S}) -> S;
40. node_state({branch, _, {leaf, S}, _}) -> S.
41.
42.
43.
44. evolve_node(PS, {leaf, S}, Rules) -> {leaf, evolve(PS, \$., S, \$., Rules)};
45. evolve_node(PS, {branch, Left, {leaf, S}, Right}, Rules) ->
46.     NLeft = evolve_node(S, Left, Rules),
47.     NRight = evolve_node(S, Right, Rules),
48.     NS = evolve(PS, node_state(Left), S, node_state(Right), Rules),
49.     {branch, NLeft, {leaf, NS}, NRight}.
50.
51. evolve_tree(T, Rules) ->
52.     evolve_node(\$., T, Rules).
53.
54. %print_tree({leaf, S}) -> io:format("~c ",[S]);
55. %print_tree({branch, Left, Node, Right}) ->
56. %    io:format("( ",[]),
57. %    print_tree(Left),
58. %    print_tree(Node),
59. %    print_tree(Right),
60. %    io:format(") ",[]).
61.
62. print_value_path(Tree, Path) ->
63.     SPath = lists:filter(fun(C) -> ((C =/= \$[) and (C =/= \$])) end, Path),
64.     Node = lists:foldl(fun(D, {branch, Left, _, Right}) ->
65.                         case D =:= \$> of
66.                             true -> Right;
67.                             false -> Left
68.                         end
69.                       end, Tree, SPath),
70.     io:format("~c~n", [node_state(Node)]).
71.
72. process_query(Trees, Rules) ->
73.     {ok, [Step, Path]} = io:fread("","~d ~s"),
74.     [Tree|_] = NTrees = case Step >= 0 of
75.         true ->
76.             lists:foldl(fun(_, AccTrees) ->
77.                             [T|_] = AccTrees,
78.                             [evolve_tree(T, Rules)|AccTrees]
79.                             end, Trees, lists:seq(1, Step));
80.         false ->
81.             lists:foldl(fun(_, AccTrees) ->
82.                            [_|RTrees] = AccTrees,
83.                            RTrees
84.                            end, Trees, lists:seq(Step, -1))
85.      end,
86.      print_value_path(Tree, Path),
87.      NTrees.
88.
89.
90. process_queries(NQ, Tree0, Rules) ->
91.     lists:foldl(fun(_, Trees) ->
92.                process_query(Trees, Rules)
93.                end, [Tree0], lists:seq(1, NQ)).
94.
95. main() ->
96.     {ok, [RuleIndex]} = io:fread("", "~d"),
97.     Rules = rules_results(RuleIndex, 0, []),
98.     Tree0 = input_tree(),
99.     {ok, [NQ]} = io:fread("", "~d"),
100.     process_queries(NQ, Tree0, Rules).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top