# Python 2.7 code; 7th, 8th of September 2017

# import
import re

# =======
#  SETUP 
# =======

# Best expression found: 6793**164

# expression types
expressions = [
	# finds `-9114**28`
	#"aaaa**bb"
	
	# finds `6793**164`
	#"aaaa**bbb"
	
	# No smaller results
	#"aa**cc*b",
	#"a**ccc*b",
	#"a**cc**b"
	
	# No smaller results
	#"a*b**cc",
	#"a*bb**c"
	
	# No smaller results
	#"a<<b<<cc",
	#"a<<bb<<c",
	#"aa<<b<<c"
	
	# Takes a long time
	# No smaller results
	#"a**b**cc",
	#"a**bb**c",
	#"aa**b**c"
	
	# No smaller results
	#"a<<bbbbb",
	#"aa<<bbbb",
	#"aaa<<bbb",
	#"aaaa<<bb",
	#"aaaaa<<b"
	
	# No smaller results
	#"a**bbbbb",
	#"aa**bbbb",
	#"aaa**bbb",
	#"aaaa**bb",
	#"aaaaa**b"
]

# pattern to be matched
pattern = "4......3.2..56..1.......0"

# pattern offset (where in the number the first pattern character should be)
offset = 4

# maximum expression length
targetlength = 8

# possible characters for variables
vars = "abcdefghijklmnopqrstuvwxyz"

# brute force!
def bruteforce(expression):
	# find repeating variables
	expr = [expression[0]]
	for c in expression[1:]:
		if expr[-1][0] == c: expr[-1] += c
		else: expr.append(c)
	
	# build for loops
	indent = ""
	for e in expr:
		if e[0] in vars: print "%sfor %s in range(%d):" % (indent, e, 10**len(e)); indent += "\t"
	
	# expression to determine length, print out
	lenexpr = ""
	for e in expr:
		if e[0] in vars: lenexpr += "str(%s)+" % e
		else: lenexpr += "\"%s\"+" % e
	lenexpr += "(\"+\"+str(o))*(o!=0)"
	
	# build body
	print "%ss = str(%s)" % (indent, expression)
	print "%sr = re.search(pattern, s)" % indent
	print "%sif r:" % indent
	print "%s\to = r.span()[0]-offset" % indent
	print "%s\tl = %s" % (indent, lenexpr)
	print "%s\tif o == -1: l = \"-\"+l[:-3]" % indent
	print "%s\tprint \"Found [%%d]; %%s\" %% (len(l), l)" % indent
	print "%s\tif len(l) <= targetlength: print \"!\"" % indent

# main function
def main():
	# setup
	print "import re"
	print "pattern = \"%s\"" % pattern
	print "offset = %s" % offset
	print "targetlength = %s" % targetlength
	
	# brute force expressions
	for expression in expressions: bruteforce(expression)

# execute if main
if __name__ == "__main__": main()