You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

  1. # coding: utf-8
  2. import os, sys
  3. from z3 import *
  4. # get the playground information
  5. if len(sys.argv) != 2:
  6. print("Usage: python3 square.py <test-file>")
  7. sys.exit(0)
  8. with open(sys.argv[1]) as f:
  9. playground = f.read()
  10. rows = playground.strip().split("\n")
  11. playground = [[None if x == "_" else int(x) for x in r.split()] for r in rows]
  12. # get the playground size
  13. size_y = len(playground)
  14. assert(size_y != 0)
  15. size_x = len(playground[0])
  16. assert(size_x != 0)
  17. assert(size_x == size_y)
  18. #################################### Square ####################################
  19. # create the solver
  20. solver = Solver()
  21. # hint: when creating Int(...) variables for the cells, use something like the coordinates as part of the variable name
  22. numbers = [[None for _j in range(size_x)] for _j in range(size_y)]
  23. # call the solver and check satisfiability
  24. res = solver.check()
  25. if res != sat:
  26. print("unsat")
  27. sys.exit(1)
  28. # print the model
  29. m = solver.model()
  30. for i in range(size_y):
  31. results = []
  32. for j in range(size_x):
  33. num = numbers[i][j]
  34. results.append("_" if num is None else m[num].as_long())
  35. print(("%4s" * len(results)) % tuple(results))
  36. ################################################################################