# ------------------------------------------------------------
# PSUT IoT Engineering Studio | CR35
# Experiment: modular clock and residue classes
# Purpose: expose algorithm states and verify the experiment invariant.
# ------------------------------------------------------------
# CR35 | Modular clock and residue classes
# Execute the named operation; expose states and assert the laboratory invariant.
import json



DEFAULT={'a': 35, 'b': 9, 'n': 17}
ALTERNATE={'a': -7}

def run(overrides=None):
    # Merge only explicitly supplied inputs with the lecture baseline.
    params={**DEFAULT, **(overrides or {})}
    a=params['a']
    b=params['b']
    n=params['n']
    q,r=divmod(a,n)
    steps=[{'a':a,'q':q,'n':n,'r':r},{'operation':'addition','raw':a+b,'residue':(a+b)%n},{'operation':'subtraction','raw':a-b,'residue':(a-b)%n},{'operation':'multiplication','raw':a*b,'residue':a*b%n}]
    result={'residue':r,'identity':q*n+r==a,'sum':(a+b)%n,'product':a*b%n}
    # Independently check the stated mathematical or protocol invariant.
    assert result["identity"] and result["residue"]==a%n, "verification failed"
    return {"steps":steps,"result":result}

if __name__ == "__main__":
    import argparse
    parser=argparse.ArgumentParser();parser.add_argument("--inputs",default="{}");parser.add_argument("--test",action="store_true");args=parser.parse_args()
    if args.test:
        for case in [{},ALTERNATE]:run(case)
        print("PASS")
    else:print(json.dumps(run(json.loads(args.inputs)),indent=2))
