23 lines
625 B
Elixir
23 lines
625 B
Elixir
|
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
|