"""Count the edge-sum multisets of seven double-six domino frames.""" from itertools import combinations_with_replacement, product from functools import cache import sys def blocks(): tile = {p: i for i, p in enumerate(combinations_with_replacement(range(7), 2))} frames = set() for s in range(19): for corners in product(range(7), repeat=4): sides = [s-corners[i]-corners[(i+1)%4] for i in range(4)] if min(sides)<0 or max(sides)>6: continue indices = [tile[tuple(sorted((a,b)))] for a,b in zip(corners,sides)] if len(set(indices))==4: frames.add((sum(1<>i)&1] for i in range(28)] order=sorted(range(28),key=lambda i:(len(pools[i]),-i if reverse_ties else i)) full=(1<<28)-1 # Four bits store each multiplicity (there are just seven frames). answers=set() completions=0 @cache def tail(left): if not left: return (0,) i=next(i for i in order if (left>>i)&1) return tuple({(1<<(4*s))+v for mask,s in pools[i] if mask&left==mask for v in tail(left^mask)}) def visit(left,hist,depth): nonlocal completions if depth==4: for v in tail(left): answers.add(hist+v) completions+=1 return i=next(i for i in order if (left>>i)&1) for mask,s in pools[i]: if mask&left==mask: visit(left^mask,hist+(1<<(4*s)),depth+1) print('frames',len(frames),'per domino', [len(pools[i]) for i in order],flush=True) visit(full,0,0) from collections import Counter distribution=Counter(next(s for s in range(19) if (x>>(4*s))&15) for x in answers) sample=sum(1<<(4*s) for s in [16,13,12,10,6,6,2]) assert sample in answers print('distinct',len(answers),'smallest edge sum histogram',sorted(distribution.items()),flush=True) assert len(answers)==10821 assert sorted(distribution.items())==[(2,3323),(3,2721),(4,2721),(5,1511),(6,545)] return answers if __name__=='__main__': direct=blocks() assert len(direct)==1070 alternate=check_blocks_independently() assert direct==alternate first=solve(direct) second=solve(alternate,reverse_ties=True) assert first==second for h in first: mirror=sum(((h>>(4*s))&15)<<(4*(18-s)) for s in range(19)) assert mirror in first print('Independent frame generation, second covering order and reflected tuples agree.')