"""Production table, literal step, and snapshot decoder.""" COMMAND = frozenset("bijrlm") RULES = dict(zip( "abdefgijklmuphr", ("rrd", "ahaa", "bbrrr", "g", "r", "ij", "ehee", "rhke", "frgg", "uruuruff", "r".join("uu" + "fu" * 7), "p", "lmlr", "h", ""), strict=True, )) def step(word): return None if len(word) < 2 else word[2:] + RULES[word[0]] def observe_word(word): """Read the current ordinary word directly, with no bit packing or control state.""" runs = [] length = 0 for letter in word: if letter not in COMMAND: return None if len(runs) == 3: continue # Still validate the entire suffix. if letter == "m": length += 1 elif letter in "bij" and length: runs.append(length) length = 0 if length: runs.append(length) if len(runs) < 3 or runs[:2] != [8, 2]: return None n = runs[2] k = n.bit_length() - 1 if n != 1 << k or k < 5 or (k - 5) % 4: return None return (k - 5) // 4