Add Dominoes exercise for Elixir track
This commit is contained in:
22
elixir/dominoes/lib/dominoes.ex
Normal file
22
elixir/dominoes/lib/dominoes.ex
Normal file
@@ -0,0 +1,22 @@
|
||||
defmodule Dominoes do
|
||||
@type domino :: {1..6, 1..6}
|
||||
|
||||
@doc """
|
||||
chain?/1 takes a list of domino stones and returns boolean indicating if it's
|
||||
possible to make a full chain
|
||||
"""
|
||||
@spec chain?(dominoes :: [domino]) :: boolean
|
||||
def chain?([]), do: true
|
||||
def chain?([{first, second}]) when first == second, do: true
|
||||
|
||||
def chain?([{first, second} | dominoes]) do
|
||||
Enum.any?(
|
||||
dominoes,
|
||||
fn
|
||||
{^first, x} = domino -> chain?([{second, x} | List.delete(dominoes, domino)])
|
||||
{x, ^first} = domino -> chain?([{second, x} | List.delete(dominoes, domino)])
|
||||
_ -> false
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user