Skip to content

Instantly share code, notes, and snippets.

@mrdougwright
Created September 6, 2019 23:25
Show Gist options
  • Save mrdougwright/661053080ad33fd6bb066da47d4ec14d to your computer and use it in GitHub Desktop.
Save mrdougwright/661053080ad33fd6bb066da47d4ec14d to your computer and use it in GitHub Desktop.
rock, paper, scissors
# 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