
    UHj<                    p   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
  ee      j                         j                  d   Zedz  ZdZd	Zd
ZdZdZg dZg dZdg dddddgddg dddg dddg ddgZd&dZd'dZd(dZd)dZd*dZd+d,d Zd-d!Zd-d"Zefd.d#Z d/d$Z!e"d%k(  r e!        yy)0zCDecompose selector 2:0 field maps against previous route selectors.    )annotationsN)Path)Any   outmap1_01amap2_02dz0:0z1:0z2:0)zsource-current-control-flowzpredecessor-state-persistencez*real-selector-2:0-or-selected-root-runtimezstrict-source-hotspot)zQVM/control-flow execution from source-side selector 0:0 into current selector 2:0zRtarget-side predecessor 1:0 state persistence into the current 2:0 frontier readerzEreal selector 2:0 gameplay savedata or selected-pointer runtime tracez9strict map1_01a source coordinate or hotspot for map2_02dzout/save_scene_selectors.json)groupslotselectedPointerHex	fieldMaps)pathfieldszout/playable_progress.jsonreachableFromStartreachableCountz2out/save_selector_predecessor_persistence_gap.json)persistenceProvenselectorMergeGapOpenrouteOrderProvenz2out/save_selector_selected_root_execution_gap.json)selectedRootExecutionRefFound
proofFoundmissingEvidencez/out/map1_01a_strict_source_hotspot_context.json)r   strictSourceCoordinateFoundtileHotspotConfirmedc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     F/home/exedev/hwanse/tools/summarize_save_selector_set_decomposition.py	load_jsonr$   8   s*    ;;=::dnngn677    c                L    | j                  d       d| j                  d       S )Nr
   :r   )get)rows    r#   selector_keyr*   >   s&    gggq 122r%   c                R    | D ]  }t        |      |k(  s|c S  t        d| d      )Nz	selector z
 not found)r*   
ValueError)	selectorskeyr)   s      r#   find_selectorr/   B   s9     #J yZ0
11r%   c                    | j                  d      xs g }t        |      }t        |       | j                  d      |t        |v t        |v ||k  t        ||z
        t        ||z
        t        ||z        d	S )Nr   r   )	selectorrootHexr   containsSourcecontainsTargetisSubsetOfCurrentextraVsCurrentmissingVsCurrentconfirmedOverlap)r(   setr*   SOURCETARGETsorted)r)   current_mapsconfirmed_maps
field_maps	field_sets        r#   selector_recordrA   I   s    %+JJI %77/0 I- I-&,6 \!9:"<)#;<"9~#=>
 
r%   c           
     j   t        | j                  d      xs g       }t        |j                  d      xs g       }||z  }t        |       t        |      g| j                  d      |j                  d      gt        |      ||k(  ||k  t        ||z
        t        ||z
        t        |v t
        |v d	S )Nr   r   )	r-   roots	unionMapsunionEqualsCurrentunionCoversCurrentr6   r7   r3   r4   )r9   r(   r*   r<   r:   r;   )leftrightr=   	left_maps
right_mapsunions         r#   pair_recordrL   Y   s    DHH[)/R0IUYY{+1r2J
"E"4(,u*=>((/0%))<P2QRE]#|3*e3 !56"<%#78 E/ E/
 
r%   c                4   |xs i }t        |j                  d      xs g       }t        | t              }t        | t              }t        | t
              }|j                  d      |j                  d      f}t        |j                  d      xs g       t        |j                  d      xs g       }t        |j                  d      xs g       }| D 	cg c]:  }	|	j                  d      r'|	j                  d      |	j                  d      f|k  r|	< }
}	|
D 	cg c]  }	t        |	|       }}	t        j                  |
d      D cg c]  \  }}t        ||       }}}t        d |D        i       }|D 	cg c]  }	|	j                  d      s|	 }}	|t        hz  k(  }t        |z
        }t        fd|D              }|xr6 t        |      xr) |j                  d	      d
u xr |j                  d      g k(  }dt
         dt         dt         ddj                  |      xs d dt         dt         d}i dt        dt         dt        dt        dt
        d|j                  d      d|j                  d      d|j                  d      d|j                  d      xs g d|j                  d      xs g d |j                  d      xs g d!t        |      d"|d#|d$t#        |      d%|d&|j                  d	      i d'|j                  d(      xs g d)|j                  d      xs g d*|d+|d,|d-|d.d/d0d/d1d/d2d/d3t$        d4t&        d5t(        d6t#        t(              d7d8d9t&        d:|S c c}	w c c}	w c c}}w c c}	w );Nr   r
   r   r      c              3  \   K   | ]$  }|j                  d       t        t        gk(  r| & yw)r-   N)r(   SOURCE_SELECTORPREDECESSOR_SELECTOR.0r)   s     r#   	<genexpr>z build_summary.<locals>.<genexpr>}   s/      	
ww{#9M'NN 	
s   *,rE   c              3  &   K   | ]  }|v 
 y wN )rS   itemr=   s     r#   rT   z build_summary.<locals>.<genexpr>   s     -i4d,.F-is   rF   Tr7   z	Selector z: can be explained as the target-side predecessor selector z plus zA, while omitting the source selector's extra confirmed-start map , nonez. The previous +aW   union covers the current selector but over-covers it by that extra source-side map, and no previous selector pair exactly equals the current route-pair map set. This supports a list-recomposition shape, but it still does not prove that gameplay executes 0:0, then 1:0, then 2:0 or that the map1_01a->map2_02d edge has a strict source hotspot.sourcetargetsourceSelectorpredecessorSelectorcurrentSelectorsourceRootHexr   predecessorRootHexcurrentRootHex
sourceMapspredecessorMapscurrentMapsconfirmedReachableFromStartpreviousSelectorspreviousPairUnionsexactPreviousSelectorUnionCountexactPreviousSelectorUnions#sourcePredecessorUnionCoversCurrentsourcePredecessorUnionExtraMapsr6   !sourcePredecessorUnionMissingMaps"currentEqualsPredecessorPlusSource'sourceSelectorExtraMapsOmittedByCurrent#currentOmitsSourceSelectorExtraMapslistRecompositionPatternFoundexecutionOrderProvenFstrictHotspotFoundr   "selectorSetDecompositionProofFound%failedSelectorSetDecompositionGateIdsr   evidenceRefsevidenceRefCountpromotionStatusblockedremainingProofs
conclusion)r9   r(   r/   rP   rQ   CURRENT_SELECTORrA   	itertoolscombinationsrL   nextr:   r<   allbooljoinr;   len*FAILED_SELECTOR_SET_DECOMPOSITION_GATE_IDS+SELECTOR_SET_DECOMPOSITION_MISSING_EVIDENCE(SELECTOR_SET_DECOMPOSITION_EVIDENCE_REFS)r-   playable_progressr>   
source_rowpredecessor_rowcurrent_rowcurrent_orderpredecessor_mapssource_mapsr)   previous_rowsprevious_recordsrG   rH   	pair_rowssource_predecessor_pairexact_pairs&current_equals_predecessor_plus_sourcesource_selector_extra#current_omits_source_selector_extra list_recomposition_pattern_foundr|   r=   s                         @r#   build_summaryr   j   s   )/R*../CDJKNy/:J#I/CDO	+;<K __W-{v/FGM{{39r:L?..{;ArBjnn[17R8K 77;WWWswwv/-? 	M  Wdds\>JddKTKaKaboqrKstKD%T5,7tIt"	
$	
 	 #,M3sww7K/L3MKM-9>NRXQY>Y-Z*";#=>*--iSh-i*i'. 	B&'	B#''(<=E	B $''(:;rA	 % $%%_
 vh.o99*+5v
6ooEVVWXlWm n!	! #&#&# 	/# 	3	#
 	+# 	(<=# 	o112FG# 	+//*>?# 	jnn[17R# 	?..{;Ar# 	{{39r# 	&vn'=# 	-# 	i# 	*3{+;#  	&{!#" 	./F/J/JK_/`##$ 	*+B+F+FGW+X+^\^%#& 	,-D-H-HI[-\-b`b'#( 	-.T)#* 	23H+#, 	./R-#. 	()I/#0 	1#2 	e3#4 	e5#6 	-e7#8 	01[9#: 	F;#< 	@=#> 	C HI?#@ 	9A#B 	FC#D 	jE# #E et Ns   ?NN

N;NNc                R   ddd| d    d| d    dd| d	    d
| d    dd| d    d
| d    dd| d    d
| d    dd| d    d| d    ddj                  | d         xs d d| d    d| d    d| d    d | d!    d"dj                  | d#          d$t        | d%          d&| j                  d'       d(| d)    dd| d*   dd+dd,d-g}| d.   D ]  }|j                  d/|d0    d1|d2    d3dj                  |d4          d5|d6    d5dj                  |d7         xs d8 d5dj                  |d9         xs d8 d5dj                  |d:         xs d8 d;        |j	                  g d<       | d=   D ]d  }|j                  d/|d>   d?    d@|d>   dA    d3|dB    d5|dC    d5dj                  |d7         xs d8 d5dj                  |d9         xs d8 d;       f |j	                  g dD       |j	                  dE | dF   D               |j                  d       dGj                  |      S )HNz!# Save Selector Set Decomposition z
- route: `r\    -> r]   `z- source selector: `r^   z` root `ra   z- predecessor selector: `r_   rb   z- current selector: `r`   rc   z*- current equals predecessor plus source: ro   z+- source+predecessor union covers current: rl   z'- source+predecessor union extra maps: rY   rm   rZ   z'- exact previous selector union count: rj   z$- list recomposition pattern found: rr   z- execution order proven: rs   z- proof found: r   z+- failed selector-set decomposition gates: rv   z- missing evidence count: r   z- evidence refs: rx   z- promotion status: `ry   r|   z## Previous SelectorszT| selector | root | maps | subset of current | extra | missing | confirmed overlap |z+| --- | --- | --- | --- | --- | --- | --- |rh   z| `r1   z` | `r2   z` | r   z | r5   r6   -r7   r8   z |)r   z## Pair Unionsr   zA| selectors | equals current | covers current | extra | missing |z| --- | --- | --- | --- | --- |ri   r-   r   z` + `r   rE   rF   )r   z## Remaining Proofsr   c              3  &   K   | ]	  }d |   yw)z- NrW   rS   rX   s     r#   rT   zmarkdown.<locals>.<genexpr>   s     D2dVDs   r{   
)r   r   r(   appendextend)summarylinesr)   s      r#   markdownr      s9   +

WX&'tGH,=+>a@
w'789'/BZA[[\]
#G,A$B#C8GThLiKjjkl
(9 :;8GL\D]C^^_`
4W=a5b4cd
5g>c6d5ef
1$))GDe<f2g2qkq1rs
1':[2\1]^
.w7V/W.XY
$W-C%D$EF
',/01
5diiHo@p6q5rs
$S1B)C%D$EF
GKK(:;<=
(9 :;1=


^51E4 *+ 
#j/"%I'7tDIIc+FV<W;XX[&'(DIIc:J6K,L,SPS+TTWyy/018S9TYYsK]G^=_=fcf<ggik	

 
LL   +, 
#k"1%&eC,<Q,?+@SI]E^D__b'()TYYs;K7L-M-TQT,UUXyy/018S9=	

 
LL01	LLD1B)CDD	LL99Ur%   c                   dj                  d | d   D              }dj                  d | d   D              }dj                  d | d   D              }dj                  d	d
ddddddddd| d    d| d    d| d    d| d    d| d    d| d    dd | d!    d"| d#    d$t        j                  d%j                  | d&         xs d'       d(| d)    d*| d+    d,| d-    d.d/| d0    d1t        j                  d2j                  | d3                d4t        | d5          d6| j	                  d7       d	d8t        j                  | d9          d:d;d<|d=d>d?|d=d@dA| dBdCdDdg      S )ENr   c              3    K   | ]  }d t        j                  |d          dt        j                  |d          dt        j                  dj                  |d                d|d    dt        j                  dj                  |d	         xs d
       dt        j                  dj                  |d         xs d
       dt        j                  dj                  |d         xs d
       d  yw)<tr><td><code>r1   z</code></td><td><code>r2   </code></td><td>rY   r   	</td><td>r5   r6   r   r7   r8   
</td></tr>Nhtmlescaper   rR   s     r#   rT   zhtml_page.<locals>.<genexpr>   s       	[[Z12 3[[Y01 2{{499S%5678 9&'( ){{499S)9%:;BsCD E{{499S);%<=DEF G{{499S);%<=DEF G	s   C1C3rh   c              3  Z  K   | ]  }d t        j                  |d   d          dt        j                  |d   d          d|d    d|d    dt        j                  d	j                  |d
         xs d       dt        j                  d	j                  |d         xs d       d  yw)r   r-   r   z</code> + <code>r   r   rE   r   rF   rY   r6   r   r7   r   Nr   rR   s     r#   rT   zhtml_page.<locals>.<genexpr>   s      	 	[[[!1!!4566Ft{{SVWbScdeSfGgFh i'() *'() *{{499S)9%:;BsCD E{{499S);%<=DEF G		s   B)B+ri   r   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li>z</li>N)r   r   r   s     r#   rT   zhtml_page.<locals>.<genexpr>  s#     \tDKK-.e4\s   #%r{   z<!doctype html>z<html lang="en">z<head>z  <meta charset="utf-8">zF  <meta name="viewport" content="width=device-width, initial-scale=1">z0  <title>Save Selector Set Decomposition</title>a    <style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;margin:24px}table{border-collapse:collapse;width:100%;max-width:1180px}td,th{border:1px solid #333;padding:6px 8px;text-align:left;vertical-align:top}th{background:#1f1f1f}code{color:#9bd4ff}</style>z</head>z<body>z*  <h1>Save Selector Set Decomposition</h1>z  <p>route <code>r\   r   r]   z</code>; source <code>r^   z</code>; predecessor <code>r_   z</code>; current <code>r`   z </code>; promotion status <code>ry   z</code>.</p>z-  <p>current equals predecessor plus source: ro   z+; source+predecessor union covers current: rl   z'; source+predecessor union extra maps: rY   rm   rZ   z'; exact previous selector union count: rj   z$; list recomposition pattern found: rr   z; execution order proven: rs   z.</p>z  <p>proof found <code>r   z7</code>; failed selector-set decomposition gates <code>,rv   z </code>; missing evidence <code>r   z</code>; evidence refs <code>rx   z  <p>r|   z</p>z  <h2>Previous Selectors</h2>z  <table><thead><tr><th>selector</th><th>root</th><th>maps</th><th>subset</th><th>extra</th><th>missing</th><th>confirmed overlap</th></tr></thead><tbody>z  </tbody></table>z  <h2>Pair Unions</h2>z  <table><thead><tr><th>selectors</th><th>equals current</th><th>covers current</th><th>extra</th><th>missing</th></tr></thead><tbody>z  <h2>Remaining Proofs</h2>z  <ul>z</ul>z</body>z</html>)r   r   r   r   r(   )r   r   r   proofss       r#   	html_pager      s_   II  ./ M 		 	 /0	 	I WW\IZA[\\F99"P: 	\4
GH-.d783D2EE[\cdt\u[v  wR  SZ  [p  Sq  Rr  rI  JQ  Rc  Jd  Ie  eE  FM  N_  F`  Ea  am  	n
7@d8e7f  gR  SZ  [@  SA  RB  Bi  jn  ju  ju  vz  v  v  @G  Hi  @j  vk  vu  ou  jv  iw  w^  _f  gH  _I  ^J  Jn  ov  wV  oW  nX  Xr  sz  {Q  sR  rS  SX  	Y
!',"7!88optp{p{|  }E  }E  FM  Nu  Fv  }w  qx  py  yY  Z]  ^e  fw  ^x  Zy  Yz  zW  X_  Xc  Xc  dv  Xw  Wx  xD  	E
GL12348' 	e  	Q%

7  r%   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz$save_selector_set_decomposition.jsonFrN   )ensure_asciiindentr   r   r   $save_selector_set_decomposition.html)mkdir
write_textr   dumpsr   )r   out_dirs     r#   write_outputsr   '  se    MM$M.55AA

7q9D@ B  55AA)GBT_fAgr%   c                 f   t        j                  t              } | j                  dt        t
               | j                         }t        t        |j                  dz  g       t        |j                  dz  i             }t        ||j                         t        d|j                  dz          y )N)descriptionz	--out-dir)typedefaultzsave_scene_selectors.jsonzplayable_progress.jsonz)wrote save selector set decomposition -> r   )argparseArgumentParser__doc__add_argumentr   OUT
parse_argsr   r$   r   r   print)parserargsr   s      r#   mainr   0  s    $$9F
$<D$,,!<<bA$,,!992>G '4<<(	5dllEk6k5l
mnr%   __main__)r   r   r"   r   returnr   )r)   dictr   str)r-   
list[dict]r.   r   r   r   )r)   r   r=   set[str]r>   r   r   r   )rG   r   rH   r   r=   r   r   r   rV   )r-   r   r   zdict | Noner   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )#r   
__future__r   r   r   r~   r   pathlibr   typingr   __file__resolver   ROOTr   r:   r;   rP   rQ   r}   r   r   r   r$   r*   r/   rA   rL   r   r   r   r   r   __name__rW   r%   r#   <module>r      s   I "       H~''*
Ul		  . */ + 0F
 -')9:
 ES
 ET
 BW#, (0832 "Od1h4n 25 h	o zF r%   