"""Bounded C1583 replay; original spans only, civil BC/AD counting."""
from pathlib import Path
import hashlib,json
R=Path(__file__).resolve().parent
checks=[]
def inv(n):
 p=1
 while n%10==0:n//=10;p*=10
 v=0
 while n:n,d=divmod(n,10);v=10*v+d
 return p*v
def test(name,a,b):
 checks.append({'name':name,'actual':a,'expected':b,'pass':a==b})
def civil(year,era):return 1-year if era=='BC' else year
test('OriginalConquestNativity',1406-6,1400)
test('OriginalSPFloodConquest',3106-1406,1700)
test('Reversedoriginallegs',[inv(1400),inv(1700)],[4100,7100])
sp=6+inv(1400)+inv(1700)
test('SPRegularinverseFlood',sp,11206)
test('SPtoConquest',sp-1406,20*490)
test('ConquesttoAD65',civil(65,'AD')-civil(1406,'BC'),3*490)
test('SPtoAD65',civil(65,'AD')-civil(sp,'BC'),23*490)
test('20plus3partition',20*490+3*490,23*490)
mt2=1406+inv(4836-1406)
mt3=6+inv(1400)+inv(4836-1406)
test('MTselfretainedlowerleg',inv(3430),3430)
test('MTConquestheldFlood',mt2,4836)
test('MTcompletedFlood',mt3,7536)
test('MTstagechange',mt3-mt2,2700)
test('MTtoRestoration',mt3-536,7000)
test('MT70grid',(mt3-536)//70,100)
test('SPtoMTFlood',sp-mt2,13*490)
test('MTFloodtoConquest',mt2-1406,7*490)
test('MTFloodtoAD65',civil(65,'AD')-civil(mt2,'BC'),10*490)
test('13plus7plus3',[sp-mt2,mt2-1406,civil(65,'AD')-civil(1406,'BC')],[6370,3430,1470])
test('Entireorderedchain',6370+3430+1470,11270)
test('Translatedearlierendpoint',civil(14006,'BC')+2800,civil(sp,'BC'))
test('Translatedlaterendpoint',civil(2736,'BC')+2800,civil(65,'AD'))
spcum=6+inv(1400)+inv(4716-1406)
test('SPcompletedcumFlood',spcum,5436)
test('MTSPinverseFloodgap',mt3-spcum,2100)
test('Babylonianradii',[mt3-536,spcum-536],[7000,4900])
test('Commonrestorationpartition',2100+4900,7000)
test('SourceMTSPFloodgap',4836-4716,120)
test('Inversegaplocallyexplained',inv(3430)-inv(3310),2100)
assert all(c['pass'] for c in checks),[c for c in checks if not c['pass']]
result={'status':'PASS','checks':checks,'count':len(checks),'reversal_scope':'originaldurationsonly','coordinate_rule':'BC label B mappedto1-B; AD A mappedtoA'}
(R/'verification.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({'status':result['status'],'count':len(checks)}))
