#!/usr/bin/env python3 """Replay the finite checks and explicit repair schedule in the paper. This standard-library script checks the rooted orbit once and the path, repair, edge counts, and height function for 0 <= t <= --max-t. The paper and its Lean formalization give the result for all t. """ import argparse from collections import defaultdict LABELS = ("012", "102", "120", "021", "210", "201") ODD_LABELS = frozenset({1, 3, 4}) def right_multiplication(permutation): return tuple( LABELS.index("".join(label[int(position)] for position in permutation)) for label in LABELS ) A = right_multiplication("102") B = right_multiplication("120") B_INVERSE = right_multiplication("201") ROOM = ( 0, 1, 9, 16, 14, 23, 24, 25, 33, 40, 43, 51, 58, 56, 48, 47, 38, 30, 31, 39, 46, 49, 57, 59, 50, 42, 41, 32, 34, 37, 45, 52, 55, 54, 53, 44, 36, 35, 26, 28, 21, 13, 12, 11, 2, 4, 7, 15, 22, 20, 29, 27, 19, 18, 17, 8, 10, 3, 5, 6, ) ROOM_ENDPOINTS = {6, 11, 15, 16, 18, 20, 23, 28, 30, 39, 42, 44, 51} CENTRAL_BLOCK = ( 5, 3, 7, 6, 2, 4, 9, 11, 0, 1, 10, 8, 12, 13, 22, 20, 17, 15, 19, 18, 14, 16, 21, 23, 26, 30, 31, 27, 29, 32, 34, 25, 24, 35, 33, ) INITIAL_PIVOTS = (14, 21, 31, 41, 43, 53, 46) ROOM_ROTATIONS = ( ("L", 56, 65), ("L", 66, 67), ("L", 75, 77), ("L", 78, 79), ("R", 109, 106), ("L", 76, 74), ("L", 83, 81), ("L", 88, 91), ("L", 99, 101), ("L", 102, 110), ("L", 112, 115), ("R", 104, 106), ("R", 109, 108), ) SLICE_MOVES = ( (116, 123, 125, 126, 130), (126, 133, 134, 143, 141), (143, 148, 144, 152, 151), ) ROOM_EDGES_LOST = { (46, 49), (48, 56), (65, 66), (67, 75), (74, 83), (76, 79), (77, 78), (81, 88), (91, 99), (101, 102), (108, 116), (110, 112), } def check(condition, message): if not condition: raise AssertionError(message) def neighbors(code, modulus=None): height, label = divmod(code, 6) up, down = height + 1, height - 1 if modulus is not None: up, down = up % modulus, down % modulus return 6 * height + A[label], 6 * up + B[label], 6 * down + B_INVERSE[label] def edges(path): return { (min(left, right), max(left, right)) for left, right in zip(path, path[1:]) } def is_path(path, modulus=None): return len(set(path)) == len(path) and all( right in neighbors(left, modulus) for left, right in zip(path, path[1:]) ) def rotate(path, end, pivot, modulus): index = path.index(pivot) if end == "L": check(index > 1 and pivot in neighbors(path[0], modulus), "Illegal left rotation") return path[:index][::-1] + path[index:] check(end == "R" and index < len(path) - 2 and pivot in neighbors(path[-1], modulus), "Illegal right rotation") return path[:index + 1] + path[index + 1:][::-1] def verify_room_orbit(): check(set(ROOM) == set(range(60)) and is_path(ROOM), "The printed room must be a path on all ten levels") queue = [ROOM] seen = {ROOM} rotation_count = 0 endpoints = set() for path in queue: endpoints.add(path[-1]) possible_pivots = neighbors(path[-1]) check(all(0 <= pivot < 60 for pivot in possible_pivots), "A reachable free endpoint has a neighbor outside the room") for index, pivot in enumerate(path[:-2]): if pivot in possible_pivots: result = path[:index + 1] + path[index + 1:][::-1] rotation_count += 1 if result not in seen: seen.add(result) queue.append(result) check(len(seen) == 30 and rotation_count == 60 and endpoints == ROOM_ENDPOINTS, "The rooted rotation orbit does not match the paper") def reflect(code, t, modulus): height, label = divmod(code, 6) return 6 * ((-12 * t - 7 - height) % modulus) + A[label] def make_path(t, modulus): # The long way around a two-level cycle starts with a horizontal step. strip = [] current = neighbors(0, modulus)[2] cycle_steps = (0, 2, 0, 1) * 2 + (0, 2, 0) for _ in range(3 * t): strip.append(current) for step in cycle_steps: current = neighbors(current, modulus)[step] strip.append(current) current = neighbors(current, modulus)[2] central = [ 6 * ((-6 * t - 1 - code // 6) % modulus) + code % 6 for code in CENTRAL_BLOCK ] check(current == central[0] and central[-1] == reflect(current, t, modulus), "The central block does not meet the specified entries") middle = strip + central + [reflect(code, t, modulus) for code in reversed(strip)] path = list(reversed(ROOM)) + middle + [reflect(code, t, modulus) for code in ROOM] return path, middle def make_exchange(path, modulus): removed = {(48, 56), (46, 49)} inserted = {(48, 49), (39, 46)} before = edges(path) check(removed <= before and not inserted & before, "Wrong exchange edge set") after = (before - removed) | inserted adjacency = defaultdict(set) for left, right in after: check(right in neighbors(left, modulus), "Exchange inserts a nonedge") adjacency[left].add(right) adjacency[right].add(left) exchanged = [56] while len(exchanged) < len(path): next_vertices = adjacency[exchanged[-1]].difference(exchanged[-2:-1]) check(len(next_vertices) == 1, "The exchanged edge set does not form a path") exchanged.append(next(iter(next_vertices))) check(is_path(exchanged, modulus) and edges(exchanged) == after, "The reconstructed path does not use the exchanged edge set") check(before - after == removed and after - before == inserted, "The replacement must remove and insert exactly two edges") return exchanged def verify_height_function(t, modulus, missing): heights = [] for code in range(6 * modulus): height, label = divmod(code, 6) for _ in range(height % 3): label = B_INVERSE[label] correction = 2 if label == 4 else int(label not in ODD_LABELS) heights.append(max(0, min(height - 18 + correction, modulus - height))) check(all( abs(heights[code] - heights[neighbor]) <= 1 for code in range(6 * modulus) for neighbor in neighbors(code, modulus) ), "The height function is not 1-Lipschitz") check(all( heights[code] == heights[reflect(code, t, modulus)] == 0 for code in ROOM_ENDPOINTS ), "The height function must vanish at every reachable endpoint") check(heights[missing] == 6 * t + 5, "Unexpected height at the missing vertex") check(all( heights[neighbor] >= 6 * t + 4 for neighbor in neighbors(missing, modulus) ), "The height at a neighbor of the missing vertex is too small") def verify_instance(t): modulus = 12 * t + 26 missing = 130 + 36 * t path, middle = make_path(t, modulus) check(is_path(path, modulus) and set(path) == set(range(6 * modulus)) - {missing}, "The prescribed path does not span exactly the punctured graph") verify_height_function(t, modulus, missing) for pivot in INITIAL_PIVOTS: path = rotate(path, "L", pivot, modulus) before_exchange = edges(path) room_edges = edges(path[:60]) | edges(path[-60:]) repaired = make_exchange(path, modulus) for end, old, pivot in ROOM_ROTATIONS: endpoint = repaired[0] if end == "L" else repaired[-1] check(endpoint == old, "Wrong old endpoint in the room rotation schedule") repaired = rotate(repaired, end, pivot, modulus) room_word = repaired[len(middle):] check(repaired[:len(middle)] == middle[::-1] and repaired[-1] == 116, "The room rotation sequence does not leave the advertised ordering") check(room_edges - edges(room_word) == ROOM_EDGES_LOST, "The twelve lost room edges do not match the paper") check(len(before_exchange - edges(repaired)) == 13, "The intermediate exchange distance is not thirteen") for block in range(t): for moves in SLICE_MOVES: terminal, first, pivot, next_terminal, last = ( 36 * block + vertex for vertex in moves ) position = repaired.index(pivot) check( repaired[0] == first and repaired[11] == last and repaired[-1] == terminal and 0 < position < 11 and repaired[position + 1] == next_terminal, "The current slice does not fit the prescribed move", ) repaired = rotate(repaired, "R", pivot, modulus) repaired = rotate(repaired, "L", last, modulus) check(repaired[-1] == next_terminal, "Unexpected new endpoint after a slice") check(repaired[0] == 123 + 36 * t and missing in neighbors(repaired[0], modulus), "The final path does not expose the missing vertex") spanning = [missing] + repaired check(is_path(spanning, modulus) and set(spanning) == set(range(6 * modulus)), "Adding the missing vertex did not produce a Hamilton path") check(len(before_exchange - edges(repaired)) <= 6 * t + 13, "The final exchange distance exceeds the claimed upper bound") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--max-t", type=int, default=50, help="largest t to verify") args = parser.parse_args() if args.max_t < 0: parser.error("--max-t must be nonnegative") verify_room_orbit() print("Rooted orbit: 30 paths, 60 directed rotations, 13 endpoints.") for t in range(args.max_t + 1): try: verify_instance(t) except AssertionError as error: raise SystemExit(f"Verification failed at t={t}: {error}") from error print(f"Checked the explicit repair and height function for 0 <= t <= {args.max_t}.") if __name__ == "__main__": main()