Created
September 2, 2015 14:17
-
-
Save ryuone/7044af7bd09c996a7dc5 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
-module(main). | |
-export([main/0, main/1]). | |
main() -> | |
main([""]). | |
main([_]) -> | |
{N, Nv, M} = read_line(), | |
Parent = self(), | |
Pids = lists:map(fun(Mj) -> | |
spawn_link(fun() -> | |
Msg = case Mj+1 > N of | |
true -> | |
"No"; | |
_ -> | |
Move = lists:nth(Mj+1, Nv), | |
go2goal(N, Nv, Mj+Move, Move) | |
end, | |
Parent ! {self(), Msg} | |
end) | |
end, M), | |
do_receive(Pids), | |
init:stop(). | |
do_receive([]) -> ok; | |
do_receive([Pid|T]) -> | |
receive | |
{Pid, Msg} -> | |
io:format("~s~n", [Msg]), | |
do_receive(T) | |
end. | |
go2goal(N, _Nv, Mj, _Moved) when N == Mj+1 -> "Yes"; | |
go2goal(N, _Nv, Mj, _Moved) when N < Mj -> "No"; | |
go2goal(_N, _Nv, _Mj, Moved) when Moved == 0 -> "No"; | |
go2goal(N, Nv, Mj, Moved) -> | |
Move = lists:nth(Mj+1, Nv), | |
case Move + Moved of | |
0 -> "No"; | |
_ -> go2goal(N, Nv, Mj+Move, Move) | |
end. | |
%% | |
%% Read from stdin. | |
%% | |
read_line() -> | |
N = list_to_integer(trim_whitespace(io:get_line(""))), | |
Nv = nv_split(trim_whitespace(io:get_line(""))), | |
M = list_to_integer(trim_whitespace(io:get_line(""))), | |
Acc = read_line([], M), | |
{N, Nv, Acc}. | |
read_line(Acc, 0) -> | |
lists:reverse(Acc); | |
read_line(Acc, N) -> | |
Input = list_to_integer(trim_whitespace(io:get_line(""))), | |
read_line([Input|Acc], N-1). | |
trim_whitespace(Input) -> | |
[D, []] = re:replace(Input, "\\n$", "", [global]), | |
binary_to_list(D). | |
nv_split(List) -> | |
Splited = binary:split(list_to_binary(List), <<" ">>, [global]), | |
lists:map(fun(X) -> list_to_integer(binary_to_list(X)) end, Splited). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment