-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6705e58
commit 033acf5
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
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
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 } |
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
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 } |