Skip to content

Commit

Permalink
Example solution and stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanplusplus committed Oct 8, 2024
1 parent 6705e58 commit 033acf5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions exercises/practice/dominoes/.meta/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local function reversed(domino)
return { domino[2], domino[1] }
end

local function without_domino(dominoes, target)
local without = {}
for i = 1, #dominoes do
if i ~= target then
table.insert(without, dominoes[i])
end
end
return without
end

local function can_chain(dominoes)
local function recur(left, dominoes, right)
if #dominoes == 0 then
return left == right
end

for i, domino in ipairs(dominoes) do
for _, domino in ipairs({ domino, reversed(domino) }) do
if domino[1] == left and recur(domino[2], without_domino(dominoes, i), right) then
return true
end
end
end

return false
end

return recur((dominoes[1] or {})[1], without_domino(dominoes, 1), (dominoes[1] or {})[2])
end

return { can_chain = can_chain }
5 changes: 5 additions & 0 deletions exercises/practice/dominoes/dominoes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local function can_chain(dominoes)

end

return { can_chain = can_chain }

0 comments on commit 033acf5

Please sign in to comment.