Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Domino Chain by kazuki.h
from collections import defaultdict
from copy import deepcopy
def chain_counter(tiles_dict, head, cut_edge = ()):
tiles_dict_copy = deepcopy(tiles_dict)
if cut_edge:
v, w = cut_edge
tiles_dict_copy[v].discard(w)
tiles_dict_copy[w].discard(v)
if all(v == set() for v in tiles_dict_copy.values()): return 1
return sum(chain_counter(tiles_dict_copy, hip, (head, hip)) for hip in tiles_dict_copy[head])
def domino_chain(tiles):
tiles = map(lambda x: x.split("-"), tiles.split(", "))
tiles_dict = defaultdict(set)
check = [1 for i in range(8)]
for i, j in tiles:
tiles_dict[i].add(j)
tiles_dict[j].add(i)
check[int(i)] *= -1
check[int(j)] *= -1
if not check.count(-1) in {0, 2}: return 0
elif check.count(-1) == 2:
head = str(check.index(-1))
return chain_counter(tiles_dict, head)
else: return sum(chain_counter(tiles_dict, head) for head in tiles_dict.keys())//2
return result
Sept. 8, 2022
Comments: