"""本主题专属离线算法，仅读取同目录合成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']
w,h=data['xres'],data['yres'];dar=Fraction(*data['dar']);par=dar/Fraction(w,h)
tw,th=data['target'];correct_w=min(Fraction(tw),th*dar);correct_h=correct_w/dar
wrong_w=min(Fraction(tw),Fraction(th*w,h));wrong_h=wrong_w/Fraction(w,h)
assert par==Fraction(64,45) and correct_w==1280 and wrong_w==900
result={'pixel_aspect_ratio':str(par),'correct_rect':[0,0,int(correct_w),int(correct_h)],'matrix_ratio_only_rect':[int((tw-wrong_w)/2),0,int(wrong_w),int(wrong_h)]}
(BASE/'result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,ensure_ascii=False))
