% Enter your code here. Read input from STDIN. Print output to STDOUT % Your class should be named solution % https://www.hackerrank.com/contests/projecteuler/challenges/euler019 -module(solution). -export([main/0, prof/0]). %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Main %%%%%%%%%%%%%%%%%%%%%%%%%%%% main() -> Data = read_data(), Res = calculate(Data), output_data(Res), ok. %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Profiling %%%%%%%%%%%%%%%%%%%%%%%%%%%% prof() -> eprof:start(), eprof:start_profiling([self()]), main2(), eprof:stop_profiling(), eprof:analyze(total). main2() -> StartInput = os:timestamp(), Data = read_data(), io:format("INPUT: total time taken ~p seconds~n", [timer:now_diff(os:timestamp(), StartInput)/1000000]), StartRes = os:timestamp(), Res = calculate(Data), io:format("CALCULATE: total time taken ~p seconds~n", [timer:now_diff(os:timestamp(), StartRes)/1000000]), StartOutput = os:timestamp(), output_data(Res), io:format("OUTPUT: total time taken ~p seconds~n", [timer:now_diff(os:timestamp(), StartOutput)/1000000]), ok. %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Calculate %%%%%%%%%%%%%%%%%%%%%%%%%%%% calculate(Pos) -> lists:map(fun check/1, Pos). check([H|T]) -> check(T, {H, none}). check([{X, Y}|T], {{X1, Y1}, Ref2}) -> case ((X == X1) or (Y == Y1)) of true -> check(T, {{X1, Y1}, Ref2}); false -> case Ref2 of none -> check(T, {{X1, Y1}, {X, Y}}); {X2, Y2} -> case ((X == X2) or (Y == Y2)) of true -> check(T, {{X1, Y1}, Ref2}); false -> false end end end; check([], _) -> true. %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Output %%%%%%%%%%%%%%%%%%%%%%%%%%%% output_data(Ds) -> lists:map( fun(D) -> io:format("~s\n", [case D of true -> "YES"; false -> "NO" end]) end, Ds). %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Input %%%%%%%%%%%%%%%%%%%%%%%%%%%% read_data() -> Binary = read(), Res = binary:split(Binary, [<<"\n">>], [global]), [_| Tests] = [binary_to_list(R) || R <- Res], read_tests(Tests, []). read_tests([NStr | T], Acc) -> N = str2int(NStr), {Pos, RT} = read_pos(T, N, []), read_tests(RT, [Pos | Acc]); read_tests([], Acc) -> lists:reverse(Acc). read_pos(L, 0, Acc) -> {Acc, L}; read_pos([Pos | T], N, Acc) -> [X,Y] = str2intlist(Pos), read_pos(T, N - 1, [{X, Y} | Acc]). str2intlist(S) -> [str2int(T) || T <- string:tokens(S, " ")]. str2int(Str) -> element(1,string:to_integer(Str)). -define(BLK_SIZE, 16384). read() -> ok = io:setopts(standard_io, [binary]), read(<<>>). read(Acc) -> case file:read(standard_io, ?BLK_SIZE) of {ok, Data} -> read(<>); eof -> Acc end. ed(T) -> erlang:display(T).