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.

77 lines
2.3 KiB

  1. # coding: utf-8
  2. import os, sys
  3. from z3 import *
  4. import string
  5. # get the playground information
  6. directory = os.path.dirname(os.path.realpath(__file__))
  7. fname = directory + "/" + "level0.txt"
  8. if len(sys.argv) == 2:
  9. fname = sys.argv[1]
  10. with open(fname) as f:
  11. playground = f.read()
  12. playground = [[c for c in row] for row in playground.strip().split("\n")]
  13. # get the playground size
  14. size_y = len(playground)
  15. assert(size_y != 0)
  16. size_x = len(playground[0])
  17. assert(size_x != 0)
  18. for row in playground:
  19. assert(len(row) == size_x)
  20. bells = {}
  21. for i in range(size_y):
  22. for j in range(size_x):
  23. c = playground[i][j]
  24. if ord(c) in range(ord("1"), ord("9") + 1):
  25. assert(int(c) not in bells.keys())
  26. bells[int(c)] = (j, i)
  27. ################################### Maestria ###################################
  28. print(f"Playground size : {size_x} x {size_y}")
  29. print("Playground:")
  30. print("\n".join(["".join(i) for i in playground]))
  31. print("Bell positions:")
  32. print(bells)
  33. print("\n")
  34. # create the solver
  35. solver = Solver()
  36. fudge_x, fudge_y = Ints("fudge_x fudge_y")
  37. # call the solver and check satisfiability
  38. while solver.check() == sat:
  39. m = solver.model()
  40. if fudge_x is not None and fudge_y is not None:
  41. pos_x = m[fudge_x].as_long()
  42. pos_y = m[fudge_y].as_long()
  43. playground[pos_y][pos_x] = "F"
  44. solver.add(Or(fudge_x != pos_x, fudge_y != pos_y))
  45. ################################################################################
  46. from colorama import Fore, Back, Style
  47. cols = [Fore.BLUE, Fore.GREEN, Fore.RED, Fore.YELLOW, Fore.MAGENTA, Fore.CYAN]
  48. tones = [Style.BRIGHT, Style.DIM]
  49. cols = sum([[c + t for c in cols] for t in tones], [])
  50. if sys.stdout.isatty():
  51. for bell in sorted(list(bells.keys())):
  52. x, y = bells[bell]
  53. playground[y][x] = Back.BLACK + cols[bell - 1] + playground[y][x] + Style.RESET_ALL
  54. for y in range(size_y):
  55. for x in range(size_x):
  56. c = playground[y][x]
  57. if c == "_": playground[y][x] = Fore.BLACK + Back.WHITE + c + Style.RESET_ALL
  58. if c == "X": playground[y][x] = Fore.WHITE + Back.BLACK + c + Style.RESET_ALL
  59. if c == "F": playground[y][x] = Fore.BLACK + Back.WHITE + c + Style.RESET_ALL
  60. text = "\n".join(["".join([c for c in row]) for row in playground])
  61. print(text)