#!/usr/bin/env python3 """CC0. Replay a strict cap-13 MS(2) path to a primitive relator.""" from argparse import ArgumentParser START = ("AbbaBBB", "aBABab") # Each row gives the slot, operation, and expected freely reduced ordered pair. PHASE_1 = ( (0, "inv", ("bbbABBa", "aBABab")), (0, "B", ("bbABBab", "aBABab")), (1, "inv", ("bbABBab", "BAbabA")), (0, "right", ("bbABabA", "BAbabA")), (0, "A", ("AbbABab", "BAbabA")), (0, "right", ("AbbbA", "BAbabA")), (0, "inv", ("aBBBa", "BAbabA")), (1, "inv", ("aBBBa", "aBABab")), ) DETOUR = ( (0, "a", ("aaBBB", "aBABab")), (0, "A", ("aBBBa", "aBABab")), ) PHASE_2 = ( (0, "inv", ("AbbbA", "aBABab")), (1, "b", ("AbbbA", "baBABa")), (1, "right", ("AbbbA", "baBAbbA")), (0, "inv", ("aBBBa", "baBAbbA")), (1, "inv", ("aBBBa", "aBBabAB")), (1, "B", ("aBBBa", "BaBBabA")), (1, "right", ("aBBBa", "BaBBaBBa")), (1, "b", ("aBBBa", "aBBaBBaB")), ) def require(condition, detail): if not condition: raise ValueError(detail) def reduce(word): stack = [] for letter in word: require(letter in "aAbB", ("invalid letter", letter)) if stack and stack[-1] == letter.swapcase(): stack.pop() else: stack.append(letter) return "".join(stack) def replay(ak_detour=False): pair = START require(all(reduce(w) == w for w in pair), "start not freely reduced") history = [pair] first = PHASE_1 + DETOUR if ak_detour else PHASE_1 print(f" 0 start {pair} total={sum(map(len, pair))}") for working, phase in enumerate((first, PHASE_2)): fixed = 1 - working multiplier = pair[fixed] for slot, operation, expected in phase: word = pair[slot] if operation == "inv": changed = word[::-1].swapcase() elif operation == "right": require(slot == working, "right product changes multiplier") changed = reduce(word + pair[1 - slot]) elif operation in ("a", "A", "b", "B"): changed = reduce(operation + word + operation.swapcase()) else: raise ValueError(("unknown move", operation)) pair = (changed, pair[1]) if slot == 0 else (pair[0], changed) require(pair == expected, (operation, pair, expected)) total = sum(map(len, pair)) require(total <= 13, ("cap exceeded", pair)) print(f"{len(history):2d} {slot}:{operation:5s} {pair} total={total}") history.append(pair) require(pair[fixed] == multiplier, "multiplier not restored") # The end word is U^2 V, with U=aBB and V=aB. They form a free basis: # first take (U,b), then replace b by Ub=V. Replace V by U^2 V. require(pair == ("aBBBa", reduce("aBB" * 2 + "aB")), "wrong endpoint") peak = max(sum(map(len, p)) for p in history) word_peak = max(len(w) for p in history for w in p) require((peak, word_peak) == (13, 8), (peak, word_peak)) print(f"{len(history)-1} moves to a primitive; peak total {peak}, peak word {word_peak}") return history if __name__ == "__main__": parser = ArgumentParser(description=__doc__) parser.add_argument("--ak-detour", action="store_true", help="visit aaBBB and rotate back at the phase boundary") replay(parser.parse_args().ak_detour)