Sha256: 61ac19e1acee5a8abd24b5db55a5528494cf823c0e9be5b4381edb5ffbbe5703

Contents?: true

Size: 1.15 KB

Versions: 69

Compression:

Stored size: 1.15 KB

Contents

defmodule SecretHandshake do
  use Bitwise

  @codes ["wink", "double blink", "close your eyes", "jump"]

  @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
    @codes
    |> Enum.with_index()
    |> Enum.map(fn {command, i} -> {command, 2 <<< (i - 1)} end)
    |> decode_commands(code, [])
  end

  defp decode_commands([], code, results) do
    case flag_set(code, 16) do
      true -> results
      _ -> Enum.reverse(results)
    end
  end

  defp decode_commands([{command, flag} | flags], code, results) do
    case flag_set(code, flag) do
      true -> decode_commands(flags, code, [command | results])
      _ -> decode_commands(flags, code, results)
    end
  end

  defp flag_set(flags, flag_to_check), do: (flags &&& flag_to_check) == flag_to_check
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/secret-handshake/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/secret-handshake/example.exs