Skip to content

Instantly share code, notes, and snippets.

@niyaton
Created February 17, 2013 16:19
Show Gist options
  • Save niyaton/4972063 to your computer and use it in GitHub Desktop.
Save niyaton/4972063 to your computer and use it in GitHub Desktop.
import itertools
import re
org_str = '2ABACBDEDFDFDFDCBACDAFB2'
candidates = ['0', '3', '4', '5', '6', '7', '8', '9']
operators = ['+', '-', '*', '/', '=']
candidates.extend(operators)
simple_op_check = re.compile(r'[*+-/=][*+-/=0]')
for combination in itertools.combinations(''.join(candidates), 5):
for permutation in itertools.permutations('ABCDEF', 6):
replaced_str = org_str
for src, dst in zip(permutation, combination):
replaced_str = replaced_str.replace(src, dst)
replaced_str = replaced_str.replace(permutation[-1], '1')
if(simple_op_check.search(replaced_str)):
continue
equation = replaced_str.split('=')
if len(equation) != 2:
continue
ans = [eval(exp) for exp in equation]
ans = set(ans)
if len(ans) == 1:
print replaced_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment