Created
September 6, 2019 23:25
-
-
Save mrdougwright/661053080ad33fd6bb066da47d4ec14d to your computer and use it in GitHub Desktop.
rock, paper, scissors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rock, Paper, Scissors | |
# n = number of players | |
# players = string of letters "RPSPSSR" | |
defmodule RPS do | |
@winners %{"R" => "S", "S" => "P", "P" => "R"} | |
def find_winners(n, players) do | |
chars = String.split(players, "", trim: true) | |
find_the_winners(chars) | |
end | |
def find_the_winners([p1, p2 | rest]) do | |
find_winner(p1, p2) ++ find_the_winners(rest) | |
end | |
def find_the_winners(other), do: other | |
def find_winner(p1, p1), do: [] | |
def find_winner(p1, p2) do | |
case @winners[p1] == p2 do | |
true -> [p1] | |
false -> [p2] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment