Skip to content

Instantly share code, notes, and snippets.

@shellster
Last active April 21, 2017 14:40
Show Gist options
  • Save shellster/04e42465a5d6b8110732927beac950b3 to your computer and use it in GitHub Desktop.
Save shellster/04e42465a5d6b8110732927beac950b3 to your computer and use it in GitHub Desktop.
My attempt to solve the 10958 problem: https://www.youtube.com/watch?v=-ruC5A9EzzE
import itertools, sys, logging, operator, time
# RPN calculator code original borrowed from: https://codereview.stackexchange.com/questions/79795/reverse-polish-notation-calculator-in-python
def concat(a,b):
try:
if a.is_integer():
return float('%s%s' % (int(a),b))
except:
pass
raise Exception('First value is not an integer')
ops = {'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'/':operator.truediv,
'^':operator.pow,
'|':concat}
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
def calculate(equation):
stack = []
result = 0
for i in equation:
if is_number(i):
stack.insert(0,i)
else:
if len(stack) < 2:
raise Exception('Error: insufficient values in expression')
break
else:
if len(i) == 1:
n1 = float(stack.pop(1))
n2 = float(stack.pop(0))
result = ops[i](n1,n2)
stack.insert(0,str(result))
else:
n1 = float(stack.pop(0))
result = ops[i](n1)
stack.insert(0,str(result))
if result.is_integer():
return int(result)
else:
return result
locations = itertools.product(['','{} '], repeat=8)
tempequation = '1 2 {}3 {}4 {}5 {}6 {}7 {}8 {}9 {}'
i=0
for location in locations:
i+=1
equation = tempequation.format(*location)
if location.count('{} ') < 8:
equation += '{} ' * (8 - location.count('{} '))
equation = equation.strip()
if i%2 == 1:
#Due to the way we do the creation above, we end up getting doubles over every pattern.
#We take advantage of that because we actually need each pattern + the pattern with a prepended '-'
equation = '-' + equation
operations = itertools.product(['|','+','-','/','*','^'], repeat=8)
for item in operations:
forumula = equation.format(*item)
try:
result = calculate(forumula.split(' '))
print(forumula + ' = ' + str(result))
if abs(result) == 10958 or abs(int(result)) == 10958:
print('>>>>> '+ forumula + ' = ' + str(result))
with open('solution.txt', 'a') as f:
f.write(forumula + ' = ' + str(result) + '\n')
if result == 10958:
print('WE HAVE A MATCH!!!!')
time.sleep(10)
except Exception:
pass
'''
solutions.txt:
-1 2 3 4 5 6 - 7 | 8 * 9 | * - * * = -10958
1 2 3 4 5 6 - 7 | 8 * 9 | * - * * = 10958
-1 2 3 4 5 / 6 7 8 + 9 / + + ^ + - = -10958.359242546941
1 2 3 4 5 / 6 7 8 + 9 / + + ^ + + = 10958.359242546941
-1 2 3 4 5 ^ 6 | 7 8 9 + | + - + * = 10958
1 2 3 4 5 ^ 6 | 7 8 9 + | + - + * = -10958
-1 2 3 4 5 / 6 + 7 8 + 9 / + ^ + - = -10958.359242546941
1 2 3 4 5 / 6 + 7 8 + 9 / + ^ + + = 10958.359242546941
-1 2 3 4 5 + 6 | 7 | 8 9 + * / / + = 10958.333333333334
-1 2 3 4 5 ^ 6 - 7 | 8 9 / ^ * - * = 10958.46759549902
1 2 3 4 5 + 6 | 7 | 8 9 + * / / - = -10958.333333333334
1 2 3 4 5 ^ 6 - 7 | 8 9 / ^ * - * = -10958.46759549902
-1 2 3 4 / 5 6 7 8 9 / * * - ^ + * = -10958.852543901601
1 2 3 4 / 5 6 7 8 9 / * * - ^ + * = 10958.852543901601
-1 2 3 4 / 5 6 7 8 * 9 / * - ^ + * = -10958.852543901623
1 2 3 4 / 5 6 7 8 * 9 / * - ^ + * = 10958.852543901623
-1 2 3 4 / 5 6 7 * 8 9 / * - ^ + * = -10958.852543901601
1 2 3 4 / 5 6 7 * 8 9 / * - ^ + * = 10958.852543901601
-1 2 3 4 / 5 6 7 * 8 * 9 / - ^ + * = -10958.852543901623
1 2 3 4 / 5 6 7 * 8 * 9 / - ^ + * = 10958.852543901623
-1 2 3 4 * 5 * 6 - 7 8 9 * + | * * = -10958
1 2 3 4 * 5 * 6 - 7 8 9 * + | * * = 10958
-1 2 3 * 4 5 6 | 7 8 * 9 - / + ^ * = -10958.823646594843
1 2 3 * 4 5 6 | 7 8 * 9 - / + ^ * = 10958.823646594843
-1 2 3 + 4 5 ^ 6 | 7 8 9 + | + - * = 10958
-1 2 3 * 4 5 ^ 6 | 7 8 9 + | + - + = -10958
1 2 3 + 4 5 ^ 6 | 7 8 9 + | + - * = -10958
1 2 3 * 4 5 ^ 6 | 7 8 9 + | + - - = 10958
-1 2 3 / 4 5 + 6 | 7 | 8 9 + * * + = 10958.333333333332
1 2 3 / 4 5 + 6 | 7 | 8 9 + * * - = -10958.333333333332
-1 2 3 | 4 5 * 6 * 7 | 8 + 9 * + * = -10958
1 2 3 | 4 5 * 6 * 7 | 8 + 9 * + * = 10958
-1 2 3 * 4 + 5 6 7 8 + 9 | * + | + = 10958
1 2 3 * 4 + 5 6 7 8 + 9 | * + | - = -10958
-1 2 3 / 4 + 5 6 7 | 8 9 + * | / / = -10958.357142857143
1 2 3 / 4 + 5 6 7 | 8 9 + * | / / = 10958.357142857143
-1 2 3 * 4 + 5 6 + 7 8 | 9 + * | - = -10958
1 2 3 * 4 + 5 6 + 7 8 | 9 + * | + = 10958
-1 2 3 * 4 + 5 ^ 6 7 - 8 / 9 - / * = 10958.90410958904
-1 2 3 ^ 4 + 5 / 6 7 + 8 / 9 + ^ * = -10958.3317287401
1 2 3 * 4 + 5 ^ 6 7 - 8 / 9 - / * = -10958.90410958904
1 2 3 ^ 4 + 5 / 6 7 + 8 / 9 + ^ * = 10958.3317287401
-1 2 3 * 4 ^ 5 / 6 * 7 * 8 9 * + * = -10958.399999999998
1 2 3 * 4 ^ 5 / 6 * 7 * 8 9 * + * = 10958.399999999998
-1 2 3 * 4 ^ 5 * 6 / 7 + 8 + 9 | + = 10958
1 2 3 * 4 ^ 5 * 6 / 7 + 8 + 9 | - = -10958
-1 2 | 3 4 5 6 7 8 9 | / | * + * * = -10958.157303370788
1 2 | 3 4 5 6 7 8 9 | / | * + * * = 10958.157303370788
-1 2 | 3 4 5 6 * 7 8 9 + / | + * * = -10958.823529411766
1 2 | 3 4 5 6 * 7 8 9 + / | + * * = 10958.823529411766
-1 2 * 3 4 5 6 - 7 | 8 * 9 | * - * = -10958
1 2 * 3 4 5 6 - 7 | 8 * 9 | * - * = 10958
-1 2 - 3 4 5 / 6 7 8 + 9 / + + ^ - = -10958.359242546941
1 2 + 3 4 5 / 6 7 8 + 9 / + + ^ + = 10958.359242546941
-1 2 | 3 4 5 ^ 6 7 - 8 9 / / / - * = -10958.666666666666
-1 2 | 3 4 5 ^ 6 7 - 8 9 / * * - * = -10958.666666666666
1 2 | 3 4 5 ^ 6 7 - 8 9 / / / - * = 10958.666666666666
1 2 | 3 4 5 ^ 6 7 - 8 9 / * * - * = 10958.666666666666
-1 2 | 3 4 5 ^ 6 7 - 8 / 9 * / - * = -10958.666666666666
-1 2 | 3 4 5 ^ 6 7 - 8 * 9 / * - * = -10958.666666666666
1 2 | 3 4 5 ^ 6 7 - 8 / 9 * / - * = 10958.666666666666
1 2 | 3 4 5 ^ 6 7 - 8 * 9 / * - * = 10958.666666666666
-1 2 - 3 4 5 / 6 + 7 8 9 | + * | * = -10958.400000000001
-1 2 * 3 4 5 ^ 6 | 7 8 9 + | + - - = 10958
1 2 + 3 4 5 / 6 + 7 8 9 | + * | * = 10958.400000000001
1 2 * 3 4 5 ^ 6 | 7 8 9 + | + - + = -10958
-1 2 - 3 4 5 / 6 + 7 8 + 9 / + ^ - = -10958.359242546941
1 2 + 3 4 5 / 6 + 7 8 + 9 / + ^ + = 10958.359242546941
-1 2 * 3 4 5 ^ 6 - 7 | 8 9 / ^ * + = 10958.46759549902
1 2 * 3 4 5 ^ 6 - 7 | 8 9 / ^ * - = -10958.46759549902
-1 2 * 3 4 / 5 6 7 8 9 / * * - ^ - = -10958.852543901601
1 2 * 3 4 / 5 6 7 8 9 / * * - ^ + = 10958.852543901601
-1 2 * 3 4 / 5 6 7 8 * 9 / * - ^ - = -10958.852543901623
1 2 * 3 4 / 5 6 7 8 * 9 / * - ^ + = 10958.852543901623
-1 2 * 3 4 / 5 6 7 * 8 9 / * - ^ - = -10958.852543901601
1 2 * 3 4 / 5 6 7 * 8 9 / * - ^ + = 10958.852543901601
-1 2 * 3 4 / 5 6 7 * 8 * 9 / - ^ - = -10958.852543901623
1 2 * 3 4 / 5 6 7 * 8 * 9 / - ^ + = 10958.852543901623
-1 2 + 3 4 * 5 / 6 7 + 8 / 9 + ^ * = 10958.3317287401
-1 2 / 3 4 * 5 / 6 7 + 8 / 9 + ^ - = -10958.8317287401
-1 2 ^ 3 4 * 5 / 6 7 + 8 / 9 + ^ * = 10958.3317287401
1 2 - 3 4 * 5 / 6 7 + 8 / 9 + ^ * = -10958.3317287401
1 2 / 3 4 * 5 / 6 7 + 8 / 9 + ^ + = 10958.8317287401
1 2 ^ 3 4 * 5 / 6 7 + 8 / 9 + ^ * = 10958.3317287401
-1 2 * 3 4 * 5 * 6 - 7 8 9 * + | * = -10958
1 2 * 3 4 * 5 * 6 - 7 8 9 * + | * = 10958
-1 2 | 3 * 4 5 6 7 8 9 | / | * + * = -10958.157303370788
1 2 | 3 * 4 5 6 7 8 9 | / | * + * = 10958.157303370788
-1 2 | 3 * 4 5 6 * 7 8 9 + / | + * = -10958.823529411766
1 2 | 3 * 4 5 6 * 7 8 9 + / | + * = 10958.823529411766
1 2 + 3 + 4 5 6 | 7 8 * 9 - / + ^ = 10958.823646594843
1 2 * 3 * 4 5 6 | 7 8 * 9 - / + ^ = 10958.823646594843
-1 2 / 3 - 4 5 6 | 7 * 8 * 9 - + * = -10958.5
1 2 / 3 + 4 5 6 | 7 * 8 * 9 - + * = 10958.5
-1 2 + 3 - 4 5 + 6 * 7 8 9 * + | * = -10958
-1 2 * 3 - 4 5 ^ 6 | 7 8 9 + | + + = 10958
-1 2 ^ 3 - 4 5 + 6 * 7 8 9 * + | * = -10958
1 2 - 3 + 4 5 + 6 * 7 8 9 * + | * = 10958
1 2 * 3 + 4 5 ^ 6 | 7 8 9 + | + - = -10958
1 2 ^ 3 - 4 5 + 6 * 7 8 9 * + | * = -10958
-1 2 * 3 | 4 5 * 6 * 7 | 8 + 9 * - = -10958
1 2 * 3 | 4 5 * 6 * 7 | 8 + 9 * + = 10958
-1 2 + 3 * 4 * 5 / 6 7 + 8 / 9 + ^ = 10958.3317287401
-1 2 - 3 - 4 - 5 ^ 6 7 - 8 / 9 - / = 10958.90410958904
-1 2 / 3 + 4 * 5 ^ 6 7 - 8 / 9 - / = -10958.90410958904
-1 2 * 3 * 4 - 5 ^ 6 7 - 8 / 9 - / = 10958.90410958904
-1 2 ^ 3 * 4 * 5 / 6 7 + 8 / 9 + ^ = 10958.3317287401
1 2 + 3 + 4 + 5 ^ 6 7 - 8 / 9 - / = -10958.90410958904
1 2 / 3 - 4 * 5 ^ 6 7 - 8 / 9 - / = 10958.90410958904
1 2 * 3 * 4 + 5 ^ 6 7 - 8 / 9 - / = -10958.90410958904
1 2 * 3 ^ 4 + 5 / 6 7 + 8 / 9 + ^ = 10958.3317287401
1 2 ^ 3 * 4 * 5 / 6 7 + 8 / 9 + ^ = 10958.3317287401
-1 2 - 3 - 4 ^ 5 / 6 * 7 * 8 9 * + = 10958.399999999998
-1 2 * 3 * 4 ^ 5 / 6 * 7 * 8 9 * + = 10958.399999999998
1 2 + 3 + 4 ^ 5 / 6 * 7 * 8 9 * + = 10958.399999999998
1 2 * 3 * 4 ^ 5 / 6 * 7 * 8 9 * + = 10958.399999999998
-1 2 | 3 * 4 * 5 / 6 * 7 * 8 - 9 * = -10958.400000000001
1 2 | 3 * 4 * 5 / 6 * 7 * 8 + 9 * = 10958.400000000001
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment