Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
with recursion and set solution in Clear category for Domino Chain by kdim
def chains(a, t):
if not a: #End of recursion
return 0 #if there is no intersection, then return 0
elif not t: #
return 1 #if there is an intersection and the last element is empty, then the chain has converged
# b = {} if no intersection {} for {'2'} {'3','4'}
b = lambda x: {} if not a&x else (a|x)-a if (a|x)-a else x # b = (a|x)-a if intersection {'4'} for {'2'} {'2','4'}
# b = {x} if intersection and double {'5'} for {'5'} {'5'}
return sum([ chains(b(j), t[:i]+t[i+1:]) for i,j in enumerate(t) ]) #return sum of intersections
def domino_chain(tiles: str) -> int:
t = [ set(i) for i in tiles.replace(',','').replace('-','').split() ] # [{'5','3'},{'4','5'},{'5','1'} ... ]
return sum([ chains(j, t[:i]+t[i+1:] ) for i,j in enumerate(t) ]) / 2 # division by 2 since the element is counted twice
Jan. 14, 2021