Created
January 30, 2019 17:37
-
-
Save wmealing/37522a87c63edfad1f63cc51d2a135ad to your computer and use it in GitHub Desktop.
What.
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
defmodule SecretHandshake do | |
@doc """ | |
Determine the actions of a secret handshake based on the binary | |
representation of the given `code`. | |
If the following bits are set, include the corresponding action in your list | |
of commands, in order from lowest to highest. | |
1 = wink | |
10 = double blink | |
100 = close your eyes | |
1000 = jump | |
10000 = Reverse the order of the operations in the secret handshake | |
""" | |
@spec commands(code :: integer) :: list(String.t()) | |
def commands(code) do | |
seq = [32, 16, 8, 4, 2, 1] | |
m(code, seq, []) | |
end | |
def m(val, [next | seq], sentence) do | |
map = %{1 => ["wink"], 2 => "double blink", 4 => "close your eyes", 8 => "jump"} | |
cond do | |
# nothing left in the seq, just return the current sentence. | |
next == nil -> | |
sentence | |
# No remainder, means its exactly that value. | |
rem(val, next) == 0 -> | |
{:ok, tmp} = Map.fetch(map, next) | |
# ** (FunctionClauseError) no function clause matching in SecretHandshake.m/3 | |
m(val - next, seq, sentence ++ tmp) | |
# I think this means nothing was left over ? | |
true -> | |
m(val - next, seq, sentence) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment