"""本主题专属离线算法，仅读取同目录合成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']
cols,rows=data['columns'],data['rows'];tw,th=data['tile'];w,h=cols*tw,rows*th;body=[];tiles=[]
for r in range(rows):
 for c in range(cols):
  x,y=c*tw,r*th;tiles.append({'row':r,'col':c,'x_first':x,'x_last':x+tw-1,'y_first':y,'y_last':y+th-1})
  body.append(f'<rect x="{x}" y="{y}" width="{tw}" height="{th}" fill="#{"dce9f4" if (r+c)%2==0 else "f0dfc5"}"/><text x="{x+8}" y="{y+22}" font-size="12">({r},{c}) x{x}..{x+tw-1}</text>')
for x in range(w):
 if x%16==0:body.append(f'<path d="M{x} 0 V{h}" stroke="#555" stroke-width="1"/>')
for x in range(tw,w,tw):body.append(f'<path d="M{x-1} 0 V{h}" stroke="red" stroke-width="1"/><path d="M{x} 0 V{h}" stroke="blue" stroke-width="1"/>')
(BASE/'seam-grid.svg').write_text(f'<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="{h}" viewBox="0 0 {w} {h}" shape-rendering="crispEdges">'+''.join(body)+'</svg>',encoding='utf-8')
seams=[{'boundary':x,'left_last':x-1,'right_first':x,'right_plus_one_model':x+1,'right_minus_one_model':x-1} for x in range(tw,w,tw)]
assert w*h==49152 and [(x['left_last'],x['right_first']) for x in seams]==[(127,128),(255,256)]
result={'canvas':[w,h],'tiles':tiles,'seams':seams,'integer_mapping_required':True,'screen_measurement':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))
