Easy peasy, lemon squeezy
(language: Elixir)
defmodule Greed do def score_dice([_|_] = dice) do tally(dice) |> score end defp tally(enumerable) do Enum.reduce(enumerable, %{}, fn item, tally -> count = tally[item] || 0 put_in(tally[item], count+1) end) end defp score(tally, total \\ 0) defp score(%{1 => dice} = tally, total) when dice >= 3, do: score(%{tally | 1 => dice - 3}, total + 1000) defp score(%{1 => dice} = tally, total) when dice >= 1, do: score(%{tally | 1 => dice - 1}, total + 100) defp score(%{2 => dice} = tally, total) when dice >= 3, do: score(%{tally | 2 => dice - 3}, total + 200) defp score(%{3 => dice} = tally, total) when dice >= 3, do: score(%{tally | 3 => dice - 3}, total + 300) defp score(%{4 => dice} = tally, total) when dice >= 3, do: score(%{tally | 4 => dice - 3}, total + 400) defp score(%{5 => dice} = tally, total) when dice >= 3, do: score(%{tally | 5 => dice - 3}, total + 500) defp score(%{5 => dice} = tally, total) when dice >= 1, do: score(%{tally | 5 => dice - 1}, total + 50) defp score(%{6 => dice} = tally, total) when dice >= 3, do: score(%{tally | 6 => dice - 3}, total + 600) defp score(_totaled_tally, score), do: score end Greed.score_dice([1,1,1,1,1]) # 1200 Greed.score_dice([1,1,1,1,3]) # 1100 Greed.score_dice([1,1,1,3,3]) # 1000 Greed.score_dice([1,3,1,3,3]) # 500 Greed.score_dice([1,3,5,3,3]) # 450
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Easy peasy, lemon squeezy
(language: Elixir)