# Independent recomputation written by the reviewer; does not import or run bundle code.
import gzip,json,glob,datetime as dt
from fractions import Fraction as F
from collections import defaultdict
D=defaultdict(dict)
for f in sorted(glob.glob('../mr2/data/raw/*.json.gz')):
    for r in json.load(gzip.open(f))['data']:
        a=r['intensity']['actual']; t=dt.datetime.strptime(r['from'],'%Y-%m-%dT%H:%MZ')
        if a is None: continue
        s=t.hour*2+t.minute//30; d=t.date()
        if s in D[d] and D[d][s]!=a: print('conflict',d,s)
        D[d][s]=a
full={d:[D[d][s] for s in range(48)] for d in D if len(D[d])==48}
yrs=lambda y:sorted(d for d in full if d.year==y)
print('days',len(yrs(2023)),len(yrs(2024)),len(yrs(2025)))
season=lambda d:{12:0,1:0,2:0,3:1,4:1,5:1,6:2,7:2,8:2}.get(d.month,3)
prof={}
for s in range(4):
    ds=[d for d in yrs(2023) if season(d)==s]
    m=[sum(full[d][i] for d in ds) for i in range(48)]
    prof[s]=sorted(range(48),key=lambda i:(m[i],i))
out={}
for y in (2024,2025):
  for k,b in [(24,0),(24,F(1,2)),(42,0)]:
    u=F(k,48); A=Bs=0; worse=0
    for d in yrs(y):
        c=full[d]; sel=set(prof[season(d)][:k])
        a=sum((b+(1-b)*(1 if i in sel else 0))*c[i] for i in range(48))
        base=sum((b+(1-b)*u)*c[i] for i in range(48))
        A+=a; Bs+=base; worse+= a>=base; ties=0
    out[(y,k,str(b))]=(round(float(100*(1-A/Bs)),6),worse,len(yrs(y)))
for k,v in out.items(): print(k,v)
