Home > Erlang, Programming > Hacker Olympics Puzzle, Part 2

Hacker Olympics Puzzle, Part 2

I am trying to teach myself Erlang, so I gave the Hacker Olympics Puzzle another try in a complete new (to me) language.

Here are the functions that do the work, the main program with the code are in a seperate file.  Everything is in a repository on github.


decode(Code) -> convert(process(Code)).

process(Code)                            -> process(Code, [], []).
process([], Unique, _)                   -> Unique;
process([X, Y|Code], Unique, Duplicates) ->
    Pair = {X, Y},
    IsDuplicate = lists:member(Pair, Duplicates),
    IsUnique = lists:member(Pair, Unique),
    process_pair(Pair, IsDuplicate, IsUnique, Code, Unique, Duplicates).

process_pair(_, true, _, Code, Unique, Duplicates) ->
    process(Code, Unique, Duplicates);
process_pair(Pair, _, true, Code, Unique, Duplicates) ->
    process(Code, lists:delete(Pair, Unique), [Pair|Duplicates]);
process_pair(Pair, _, _, Code, Unique, Duplicates) ->
    process(Code, [Pair|Unique], Duplicates).

convert(List)               -> convert(List, []).
convert([], Result)         -> lists:concat(Result);
convert([{X, Y}|T], Result) -> convert(T, [cchar(X) + cchar(Y)|Result]).

cchar(C)           -> cchar(C, 0, "qwertyuiop").
cchar(C, I, [C|_]) -> I;
cchar(C, I, [_|T]) -> cchar(C, I + 1, T).
Categories: Erlang, Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: