fork download
  1. import random
  2. import time
  3.  
  4.  
  5. def easy_solve(num1, num2, ans):
  6. nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  7. input = (num1 + num2 + ans)
  8. nums_used = input.replace('x', '')
  9. [nums.remove(int(i)) for i in nums_used]
  10. no_answer = True
  11. while no_answer:
  12. temp_input = input[:]
  13. temp_nums = nums[:]
  14. while 'x' in temp_input:
  15. for i in range(0, len(temp_input)):
  16. if temp_input[i] == 'x':
  17. rand_num = random.randrange(0, len(temp_nums))
  18. temp_input = temp_input[:i] + str(temp_nums[rand_num]) + temp_input[i+1:]
  19. temp_nums.remove(temp_nums[rand_num])
  20. num1 = int(temp_input[0] + temp_input[1] + temp_input[2])
  21. num2 = int(temp_input[3] + temp_input[4] + temp_input[5])
  22. ans = int(temp_input[6] + temp_input[7] + temp_input[8])
  23. if num1 + num2 == ans:
  24. no_answer = False
  25. print(str(num1) + ' + ' + str(num2) + ' = ' + str(ans))
  26.  
  27.  
  28. def hard_solve(problem):
  29. split_spot = find_split(problem)
  30. sets = int(len(problem.replace(' ', '').replace('+', '').replace('=', ''))/9)
  31. nums = []
  32. for i in range(1, 10):
  33. for x in range(0, sets):
  34. nums.append(i)
  35. nums_used = problem.replace(' ', '').replace('+', '').replace('=', '').replace('x', '')
  36. [nums.remove(int(i)) for i in nums_used]
  37. no_answer = True
  38. problem = problem.replace(' ', '').replace('+', '').replace('=', '')
  39. while no_answer:
  40. temp_problem = problem[:]
  41. temp_nums = nums[:]
  42. while 'x' in temp_problem:
  43. for i in range(0, len(temp_problem)):
  44. if temp_problem[i] == 'x':
  45. rand_num = random.randrange(0, len(temp_nums))
  46. temp_problem = temp_problem[:i] + str(temp_nums[rand_num]) + temp_problem[i+1:]
  47. temp_nums.remove(temp_nums[rand_num])
  48. sums = [int(temp_problem[i:i+3]) for i in range(0, split_spot, 3)]
  49. temp_problem = temp_problem[split_spot:]
  50. ans = [int(temp_problem[i:i+3]) for i in range(0, len(temp_problem), 3)]
  51. total_sum = 0
  52. for i in sums:
  53. total_sum += i
  54. total_ans = 0
  55. for i in ans:
  56. total_ans += i
  57. if total_ans == total_sum:
  58. no_answer = False
  59. sums_string = ' + '.join(str(i) for i in sums)
  60. ans_string = ' + '.join(str(i) for i in ans)
  61. print(sums_string + " = " + ans_string)
  62.  
  63.  
  64. def find_split(problem):
  65. problem = problem.replace(' ', '').replace('+', '')
  66. for i in range(0, len(problem)):
  67. if problem[i] == '=':
  68. return i
  69.  
  70. start = time.time()
  71.  
  72. easy_solve('1xx', 'xxx', '468')
  73. easy_solve('xxx', 'x81', '9x4')
  74. easy_solve('xxx', '39x', 'x75')
  75. easy_solve('xxx', '5x1', '86x')
  76. hard_solve('xxx + xxx + 5x3 + 123 = xxx + 795')
  77. hard_solve('xxx + xxx + 23x + 571 = xxx + x82')
  78. hard_solve('xxx + xxx + xx7 + 212 = xxx + 889')
  79. hard_solve('xxx + xxx + 1x6 + 142 = xxx + 553')
  80. hard_solve('xxx + xxx + xxx + x29 + 821 = xxx + xxx + 8xx + 867')
  81. hard_solve('xxx + xxx + xxx + 4x1 + 689 = xxx + xxx + x5x + 957')
  82. hard_solve('xxx + xxx + xxx + 64x + 581 = xxx + xxx + xx2 + 623')
  83. hard_solve('xxx + xxx + xxx + x81 + 759 = xxx + xxx + 8xx + 462')
  84. hard_solve('xxx + xxx + xxx + 6x3 + 299 = xxx + xxx + x8x + 423')
  85. hard_solve('xxx + xxx + xxx + 58x + 561 = xxx + xxx + xx7 + 993')
  86.  
  87. print("--- %s seconds ---" % (time.time() - start))
Success #stdin #stdout 0.5s 12368KB
stdin
Standard input is empty
stdout
193 + 275 = 468
673 + 281 = 954
281 + 394 = 675
273 + 591 = 864
861 + 246 + 543 + 123 = 978 + 795
156 + 483 + 239 + 571 = 967 + 482
576 + 313 + 447 + 212 = 659 + 889
394 + 877 + 126 + 142 = 986 + 553
476 + 355 + 119 + 929 + 821 = 635 + 374 + 824 + 867
263 + 326 + 472 + 491 + 689 = 145 + 388 + 751 + 957
293 + 979 + 418 + 645 + 581 = 836 + 745 + 712 + 623
624 + 284 + 175 + 381 + 759 = 519 + 369 + 873 + 462
147 + 791 + 152 + 663 + 299 = 486 + 755 + 388 + 423
821 + 524 + 634 + 583 + 561 = 427 + 916 + 787 + 993
--- 0.48586344718933105 seconds ---