"""本主题专属离线算法，仅读取同目录合成JSON，不联网、不控制设备。"""
import csv, json, math, re, struct
from pathlib import Path
from fractions import Fraction
BASE=Path(__file__).resolve().parent
data=json.loads((BASE/'input.json').read_text(encoding='utf-8'))['data']
rows=[]
for name,items in data['cases'].items():
 ids=[(b>>4)&7 for b in items];direction='forward' if ids==list(range(8)) else 'reverse' if ids==list(range(7,-1,-1)) else None
 row={'case':name,'piece_ids':ids,'complete':direction is not None,'direction':direction}
 if direction:
  p={b>>4:b&15 for b in items};frame=(p[1]&1)*16+p[0];second=(p[3]&3)*16+p[2];minute=(p[5]&3)*16+p[4];hour=(p[7]&1)*16+p[6];rate=(p[7]>>1)&3
  assert second<60 and minute<60 and hour<24 and frame<[24,25,30,30][rate]
  row.update(encoded_time=f'{hour:02}:{minute:02}:{second:02}:{frame:02}',rate_code=rate,compensation_applied=False)
 else:row['reason']='未形成连续完整八片窗口'
 rows.append(row)
assert rows[0]['encoded_time']=='01:02:03:04' and not rows[1]['complete'] and rows[2]['direction']=='reverse'
result={'cases':rows,'current_video_position_claimed':False}
(BASE/'result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,ensure_ascii=False))
