"""Check both fair-mark short-row tables directly on admissible permutations.""" import json from collections import Counter from pathlib import Path SINGLE_GAPS = (1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 64) JOINT_GAPS = (1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64) def read_table(filename, gaps): table = {} for line in Path(__file__).with_name(filename).read_text().splitlines(): n, h, *bounds = map(int, line.split()) table[n, h] = dict(zip(gaps, bounds, strict=True)) return table def admissible_orders(order, remaining): if not remaining: yield order return for z in remaining: if any(x + z == 2 * y for j, y in enumerate(order) for x in order[:j]): continue yield from admissible_orders(order + [z], remaining - {z}) def main(): single = read_table('short_rows_fair_components.txt', SINGLE_GAPS) joint = read_table('short_rows_joint_doubling.txt', JOINT_GAPS) subsets = orders = prefixes = 0 for n in range(2, 10): for present in range(1, 1 << n): h = n - present.bit_count() if h > 4: continue subsets += 1 values = {i for i in range(n) if present & (1 << i)} seen = set() for order in admissible_orders([], values): orders += 1 mask = 0 seen.add(mask) for y in order: mask |= 1 << y seen.add(mask) prefixes += len(seen) for mask in seen: scores = Counter() for i in range(n): for j in range(i + 1, n): if i in values and j in values: scores[j - i] += 2 * (bool(mask & (1 << i)) != bool(mask & (1 << j))) else: scores[j - i] += 1 for gap, bound in single[n, h].items(): assert scores[gap] <= bound, ('single', n, present, mask, gap) for gap, bound in joint[n, h].items(): score = 2 * scores[gap] + 4 * scores[2 * gap] assert score <= bound, ('joint', n, present, mask, gap) print(json.dumps({'subsets': subsets, 'orders': orders, 'prefixes': prefixes})) if __name__ == '__main__': main()