"""Check the complete-row bounds against exact prefix sets through length 16.""" from collections import Counter from functools import cache from pathlib import Path from verify_density_bound import complete_rows LIMIT = 16 JOINT_GAPS = (1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64) def exact_prefixes(n): """Find every prefix on a path from the empty set to the full row.""" full = (1 << n) - 1 endpoints = [ [(1 << (y - d), 1 << (y + d)) for d in range(1, min(y, n - 1 - y) + 1)] for y in range(n) ] prefixes = {full} @cache def extensions(mask): if mask == full: return 1 count = 0 for y, pairs in enumerate(endpoints): bit = 1 << y if mask & bit: continue # At the instant y is listed, each symmetric pair must lie on one side. if all(bool(mask & left) == bool(mask & right) for left, right in pairs): count += extensions(mask | bit) if count: prefixes.add(mask) return count order_count = extensions(0) return order_count, prefixes def main(): single = complete_rows(max_length=LIMIT) joint = {} source = Path(__file__).with_name('complete_rows_joint_doubling.txt') for line in source.read_text().splitlines(): n, *bounds = map(int, line.split()) joint[n] = dict(zip(JOINT_GAPS, bounds, strict=True)) total_orders = total_prefixes = 0 for n in range(1, LIMIT + 1): order_count, prefixes = exact_prefixes(n) for mask in prefixes: marks = [bool(mask & (1 << i)) for i in range(n)] # Count unordered pairs directly, grouped by their numerical difference. counts = Counter() for i in range(n): for j in range(i + 1, n): counts[j - i] += (marks[i] != marks[j]) for gap, row in single.items(): assert counts[gap] <= row[n], ('single', n, mask, gap) for gap, bound in joint[n].items(): score = 4 * counts[gap] + 8 * counts[2 * gap] assert score <= bound, ('joint', n, mask, gap) total_orders += order_count total_prefixes += len(prefixes) print(f'Lengths 1 to {LIMIT}: {total_orders} admissible orders, ' f'{total_prefixes} distinct prefixes (summed over lengths).') print('All single- and joint-gap complete-row bounds passed.') if __name__ == '__main__': main()