Skip to content

Instantly share code, notes, and snippets.

@calebrob6
Last active December 25, 2015 00:19
Show Gist options
  • Save calebrob6/6887231 to your computer and use it in GitHub Desktop.
Save calebrob6/6887231 to your computer and use it in GitHub Desktop.
import itertools,operator
ops=[operator.add,operator.sub,operator.mul,operator.div]
opCombs = list(itertools.product(ops,ops,ops))
def customReduce(operators,nums):
total = nums[0]
for i in range(len(operators)):
if operators[i] == operator.div and nums[i+1]==0:
return 1000001
if operators[i] == operator.div and total%nums[i+1]!=0:
return 1000001
total = operators[i](total,nums[i+1])
return total
#operators[0](operators[1](operators[2](nums[0],nums[1]),nums[2]),nums[3])
f=open("4533.txt","r")
lines = [map(int,a.strip().split(" ")) for a in f if a.strip()!="0 0 0 0"]
print lines
for a in lines:
results = set()
perms = list(itertools.permutations(a,4))
for b in perms:
for c in opCombs:
results.add(customReduce(c,b))
d = sorted(list(results))
print d
longestChain=0
currentChain=0
longestChainLastItem = 1000001
lastItem = 1000001
for b in d:
if b == lastItem+1:
currentChain+=1
else:
if currentChain>longestChain:
longestChain = currentChain
longestChainLastItem = lastItem
currentChain=0
else:
currentChain=0
lastItem = b
print longestChainLastItem,longestChain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment