Skip to content

Instantly share code, notes, and snippets.

@cooper6581
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cooper6581/19f5c2e18f00855a8094 to your computer and use it in GitHub Desktop.
Save cooper6581/19f5c2e18f00855a8094 to your computer and use it in GitHub Desktop.
Daily #183 - Easy
%#!/usr/bin/env escript
-mode(compile).
read_stdin() ->
case io:get_line("") of
eof ->
io:format("Error: Recieved eof, expected line~n");
Res ->
read_stdin(list_to_integer(string:strip(Res, right, $\n)), [])
end.
read_stdin(0, Acc) ->
lists:reverse(Acc);
read_stdin(N, Acc) ->
case io:get_line("") of
eof ->
io:format("Error: Expected ~p more lines~n", [N]);
Res ->
read_stdin(N-1, [string:strip(Res, right, $\n) | Acc])
end.
sort_versions(A, B) ->
Maj_A = list_to_integer(lists:nth(1, A)),
Maj_B = list_to_integer(lists:nth(1, B)),
case Maj_A =:= Maj_B of
true ->
Min_A = list_to_integer(lists:nth(2, A)),
Min_B = list_to_integer(lists:nth(2, B)),
case Min_A =:= Min_B of
true ->
compare_rest(lists:nth(3, A), lists:nth(3, B));
false ->
Min_A < Min_B
end;
false ->
Maj_A < Maj_B
end.
compare_rest(A, B) ->
{Patch_A, Patch_B} = get_patch(A, B),
case Patch_A =:= Patch_B of
true ->
length(string:tokens(A, "-")) =/= 1;
false ->
Patch_A < Patch_B
end.
get_patch(A, B) ->
{list_to_integer(lists:nth(1, string:tokens(A, "-+"))),
list_to_integer(lists:nth(1, string:tokens(B, "-+")))}.
main(_) ->
Input = read_stdin(),
Split = lists:map(fun(X) -> string:tokens(X, ".") end, Input),
io:format("~p~n", [lists:sort(fun sort_versions/2, Split)]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment