Advertisement
Guest User

Untitled

a guest
Dec 24th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. from z3 import *
  2.  
  3. set_param('parallel.enable',True)
  4.  
  5. inps=[Int(f"inp{i+1}") for i in range(14)]
  6.  
  7. curr_inppos=0
  8. next_idx={"x": 0, "y": 0, "z": 0, "w": 0}
  9. curr_v_m={}
  10.  
  11. s=Then('simplify','psmt').solver()
  12.  
  13. # Assert the given boolean clause
  14. def c(clause):
  15.     s.add(clause)
  16. # Generate a new numbered var for the given base var (x1, x2, ...)
  17. def new_v(base):
  18.     curr_v_m[base]=Int(f"{base}{next_idx[base]}")
  19.     next_idx[base]+=1
  20.     return curr_v(base)
  21. # Get the current numbered var for a given base var
  22. def curr_v(v):
  23.     if v in ["x","y","z","w"]:
  24.         return curr_v_m[v]
  25.     else:
  26.         return int(v)
  27.  
  28. # Inputs are in range 1..9
  29. for v in inps:
  30.     c(v>=1)
  31.     c(v<=9)
  32.  
  33. # Initialize x,y,z,w to zero
  34. c(new_v("x")==0)
  35. c(new_v("y")==0)
  36. c(new_v("z")==0)
  37. c(new_v("w")==0)
  38.  
  39. # Construct formula according to input
  40. with open("in") as f:
  41.     for l in f.readlines():
  42.         lv=l.strip().split(" ")
  43.         a=curr_v(lv[1])
  44.         b=0
  45.         if len(lv)>2:
  46.             b=curr_v(lv[2])
  47.         if lv[0]=="inp":
  48.             c(new_v(lv[1])==inps[curr_inppos])
  49.             curr_inppos+=1
  50.         elif lv[0]=="add":
  51.             c(new_v(lv[1])==a+b)
  52.         elif lv[0]=="mul":
  53.             c(new_v(lv[1])==a*b)
  54.         elif lv[0]=="eql":
  55.             nv=new_v(lv[1])
  56.             c(Implies(a==b,nv==1))
  57.             c(Implies(a!=b,nv==0))
  58.         elif lv[0]=="mod":
  59.             c(a>=0)
  60.             c(b>0)
  61.             c(new_v(lv[1])==a%b)
  62.         elif lv[0]=="div":
  63.             c(b!=0)
  64.             c(new_v(lv[1])==a/b)
  65.         else:
  66.             raise("??!")
  67.  
  68. # Should end with z=0
  69. c(curr_v("z")==0)
  70.  
  71. # Maximize by fixing one digit at a time
  72. print('1: ',end='')
  73. s.push()
  74. for v in inps:
  75.     for i in range(10,0,-1):
  76.         s.push()
  77.         c(v==i)
  78.         if s.check()==sat:
  79.             s.pop()
  80.             c(v==i)
  81.             print(str(i),end='')
  82.             break
  83.         else:
  84.             s.pop()
  85.             continue
  86. s.pop()
  87. # Minimize by fixing one digit at a time
  88. print('\n2: ',end='')
  89. s.push()
  90. for v in inps:
  91.     for i in range(1,10):
  92.         s.push()
  93.         c(v==i)
  94.         if s.check()==sat:
  95.             s.pop()
  96.             c(v==i)
  97.             print(str(i),end='')
  98.             break
  99.         else:
  100.             s.pop()
  101.             continue
  102. s.pop()
  103. print('')
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement