Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Last active August 17, 2019 06:31
Show Gist options
  • Save KamilLelonek/c7f8be0b080694e7a5a411107eacf2bc to your computer and use it in GitHub Desktop.
Save KamilLelonek/c7f8be0b080694e7a5a411107eacf2bc to your computer and use it in GitHub Desktop.
defmodule Colors do
@colors ~w(R G B)
def calculate(string)
when byte_size(string) in [0, 1],
do: string
def calculate(string) do
string
|> String.graphemes()
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map_join(&mix_colors/1)
|> calculate()
end
defp mix_colors([c, c]), do: c
defp mix_colors([c1, c2]) do
@colors
|> Kernel.--([c1, c2])
|> List.last()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment