fork download
  1. from time import clock
  2. from random import randint
  3.  
  4. class mathagram(object):
  5.  
  6. def __init__(self, first, second, third,
  7. fourth=None, fifth=None, sixth=None,
  8. seventh=None, eighth=None, ninth=None):
  9. self.level = 0
  10. self.unused_nums = range(1, 10)
  11. self.input = [first, second, third, fourth, fifth, sixth, seventh, eighth, ninth]
  12. if self.input.count(None) == 6:
  13. self.level = 1
  14. elif self.input.count(None) == 3:
  15. self.level = 2
  16. else:
  17. self.level = 3
  18. self.unused_nums *= self.level
  19. #remove used numbers
  20. for opt in self.input:
  21. if not opt is None:
  22. for char_ in opt:
  23. try:
  24. if int(char_) in self.unused_nums:
  25. self.unused_nums.remove(int(char_))
  26. except ValueError:
  27. pass
  28. self.unused_nums.sort() # sorted for shits and giggles
  29.  
  30. def guess(self):
  31. abc = []
  32. if self.level >= 1:
  33. a, b, c = list(self.input[0:3])
  34. abc.extend([a, b, c])
  35. if self.level >= 2:
  36. d, e, f = list(self.input[3:6])
  37. abc.extend([d, e, f,])
  38. if self.level == 3:
  39. g, h, i = list(self.input[6:9])
  40. abc.extend([g, h, i])
  41. # basically replaces 'x' with a random unused number
  42. for idx,currStr in enumerate(abc):
  43. if currStr != None:
  44. currStr = list(currStr)
  45. for idx_, int_ in enumerate(currStr):
  46. if int_ == 'x':
  47. guess = randint(0, (len(self.unused_nums)-1))
  48. replace = self.unused_nums[guess]
  49. currStr[idx_] = str(replace)
  50. self.unused_nums.remove(replace)
  51. attempt = int(''.join(currStr))
  52. abc[idx] = attempt
  53.  
  54. if self.level == 1:
  55. if (abc[0] + abc[1]) == abc[2]:
  56. print "{} + {} = {}".format(*abc[0:3])
  57. return True
  58. else:
  59. return False
  60. elif self.level == 2:
  61. if (abc[0] + abc[1] + abc[2] + abc[3]) == (abc[4] + abc[5]):
  62. print "{} + {} + {} + {} = {} + {}".format(*abc[0:6])
  63. return True
  64. else:
  65. return False
  66. elif self.level == 3:
  67. if (abc[0] + abc[1] + abc[2] + abc[3] + abc[4]) == (abc[5] + abc[6] + abc[7] + abc[8]):
  68. print "{} + {} + {} + {} + {} = {} + {} + {} + {}".format(*abc[0:10])
  69. return True
  70. else:
  71. return False
  72. # end of class
  73. # running the class to make the calculations and print pretty things
  74. programStart = clock()
  75. tests = [('1xx', 'xxx', '468'),
  76. ('xxx', 'x81', '9x4'),
  77. ('xxx', '5x1', '86x'),
  78. ('xxx', '39x', 'x75'),
  79. ('xxx', 'xxx', '5x3', '123', 'xxx', '795'),
  80. ('xxx', 'xxx', '23x', '571', 'xxx', 'x82'),
  81. ('xxx', 'xxx', 'xx7', '212', 'xxx', '889'),
  82. ('xxx', 'xxx', '1x6', '142', 'xxx', '533'),
  83. ('xxx', 'xxx', 'xxx', 'x29', '821', 'xxx', 'xxx', '8xx', '867'),
  84. ('xxx', 'xxx', 'xxx', '4x1', '689', 'xxx', 'xxx', 'x5x', '957'),
  85. ('xxx', 'xxx', 'xxx', '64x', '581', 'xxx', 'xxx', 'xx2', '623'),
  86. ('xxx', 'xxx', 'xxx', 'x81', '759', 'xxx', 'xxx', '8xx', '462'),
  87. ('xxx', 'xxx', 'xxx', '6x3', '299', 'xxx', 'xxx', 'x8x', '423'),
  88. ('xxx', 'xxx', 'xxx', '58x', '561', 'xxx', 'xxx', 'xx7', '993'),
  89. # the one below takes 5-30 minutes to solve
  90. #('xxx', 'xxx', 'xxx', 'xxx', 'xxx', '987', '944', '921', '8x5'),
  91. ('987', '978', '111', '222', '33x', 'xxx', 'xxx', 'xxx', 'xxx')
  92. ]
  93. # main guessing occurs here. Make a new object each time. Not sure if safe.
  94. for equation in tests:
  95. solved = False
  96. c = 0
  97. loopStart = clock()
  98. while solved == False:
  99. m = mathagram(*equation) # <== I learned something new!
  100. solved = m.guess()
  101. c += 1
  102. else:
  103. loopElapsed = (clock() - loopStart)
  104. print "{} attempts in {} seconds.".format(c, round(loopElapsed, 3))
  105. #finalize and beautify
  106. programElapsed = (clock() - programStart)
  107. if programElapsed > 60:
  108. pMinutes = programElapsed // 60
  109. pSeconds = programElapsed % 60
  110. print "All mathagrams calculated in {}:{} minuetes.".format(pMinutes, round(pSeconds, 3))
  111. else:
  112. print "All mathagrams calculated in {} seconds.".format(round(programElapsed, 3))
  113.  
Success #stdin #stdout 2.19s 11496KB
stdin
Standard input is empty
stdout
193 + 275 = 468
2 attempts in 0.0 seconds.
673 + 281 = 954
162 attempts in 0.006 seconds.
293 + 571 = 864
69 attempts in 0.003 seconds.
284 + 391 = 675
15 attempts in 0.001 seconds.
478 + 269 + 543 + 123 = 618 + 795
3701 attempts in 0.241 seconds.
163 + 485 + 239 + 571 = 976 + 482
8445 attempts in 0.571 seconds.
376 + 413 + 547 + 212 = 659 + 889
94 attempts in 0.006 seconds.
478 + 586 + 126 + 142 = 799 + 533
3401 attempts in 0.22 seconds.
445 + 719 + 463 + 729 + 821 = 935 + 512 + 863 + 867
1694 attempts in 0.172 seconds.
823 + 543 + 612 + 411 + 689 = 438 + 726 + 957 + 957
455 attempts in 0.046 seconds.
481 + 128 + 496 + 645 + 581 = 599 + 377 + 732 + 623
5523 attempts in 0.566 seconds.
352 + 351 + 248 + 981 + 759 = 679 + 736 + 814 + 462
292 attempts in 0.03 seconds.
191 + 356 + 587 + 673 + 299 = 844 + 657 + 182 + 423
1441 attempts in 0.165 seconds.
283 + 442 + 972 + 586 + 561 = 811 + 463 + 577 + 993
1006 attempts in 0.103 seconds.
987 + 978 + 111 + 222 + 339 = 543 + 666 + 874 + 554
566 attempts in 0.052 seconds.
All mathagrams calculated in 2.18 seconds.