Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions solutions/elixir/secret-handshake/1/lib/secret_handshake.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule SecretHandshake do
import Bitwise

@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: do_commands(code)

defp do_commands(code, actions \\ [])
defp do_commands(0, actions), do: Enum.reverse(actions)
defp do_commands(code, actions) when bor(code, 1) == code, do: do_commands(code - 1, ["wink" | actions])
defp do_commands(code, actions) when bor(code, 2) == code, do: do_commands(code - 2, ["double blink" | actions])
defp do_commands(code, actions) when bor(code, 4) == code, do: do_commands(code - 4, ["close your eyes" | actions])
defp do_commands(code, actions) when bor(code, 8) == code, do: do_commands(code - 8, ["jump" | actions])
defp do_commands(code, actions) when bor(code, 16) == code, do: actions
defp do_commands(_, _), do: []
end