From 3babe2935555a15067768b767cb05516a66a7a5e Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 7 Aug 2025 07:33:24 +0000 Subject: [PATCH] [Sync Iteration] elixir/secret-handshake/2 --- .../2/lib/secret_handshake.ex | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 solutions/elixir/secret-handshake/2/lib/secret_handshake.ex diff --git a/solutions/elixir/secret-handshake/2/lib/secret_handshake.ex b/solutions/elixir/secret-handshake/2/lib/secret_handshake.ex new file mode 100644 index 0000000..5d58f16 --- /dev/null +++ b/solutions/elixir/secret-handshake/2/lib/secret_handshake.ex @@ -0,0 +1,37 @@ +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, 0b1) == code, + do: do_commands(code - 0b1, ["wink" | actions]) + + defp do_commands(code, actions) when bor(code, 0b10) == code, + do: do_commands(code - 0b10, ["double blink" | actions]) + + defp do_commands(code, actions) when bor(code, 0b100) == code, + do: do_commands(code - 0b100, ["close your eyes" | actions]) + + defp do_commands(code, actions) when bor(code, 0b1000) == code, + do: do_commands(code - 0b1000, ["jump" | actions]) + + defp do_commands(code, actions) when bor(code, 0b10000) == code, do: actions + defp do_commands(_, _), do: [] +end