Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
optimized random solution solution in Clear category for Domino Chain by flpo
def count_chains(dominos, last_digit):
if not dominos:
return 1
count = 0
for i, domino in enumerate(dominos):
if last_digit == domino[0]:
count += count_chains(dominos[:i] + dominos[i+1:], domino[1])
if last_digit == domino[1] and domino[0] != domino[1]:
count += count_chains(dominos[:i] + dominos[i+1:], domino[0])
return count
def domino_chain(tiles):
tiles = [tile.split('-') for tile in tiles.split(', ')]
nodes = set(x for tile in tiles for x in tile )
non_loop_degrees = {i: sum(i in t for t in tiles if t[0] != t[1]) for i in nodes}
odds = [v for v, d in non_loop_degrees.items() if d % 2]
if odds:
return count_chains(tiles, min(odds, key=non_loop_degrees.__getitem__))
return sum(count_chains(tiles, n) for n in nodes) // 2
Sept. 6, 2017