Euler Problem 68

I finally got it, after months of twisting in the wind. Just used a simple permutations routine and created a custom data map to test each set.Looks like I’m switching back to Python. I’ve decided to ditch Squarespace too, to save a few bucks. I can accomplish pretty much the same here on WordPress.

#Euler68
def permutate(seq):
    if not seq:
        return [seq]
    else:
        temp = []
        for k in range(len(seq)):
            part = seq[:k] + seq[k+1:]
            for m in permutate(part):
                temp.append(seq[k:k+1] + m)
        return temp
print "Calculating Permutations of 1234567890"
list = permutate("1234567890")
print "Done. Size:" + str(len(list))
print "Calculating Maximum Set"
max = 0L
for set in list:
    n = []
    n.append(int(set[5]))
    n.append(int(set[3]))
    n.append(int(set[2]))
    n.append(int(set[6]))
    n.append(int(set[2]))
    n.append(int(set[1]))
    n.append(int(set[7]))
    n.append(int(set[1]))
    n.append(int(set[0]))
    n.append(int(set[8]))
    n.append(int(set[0]))
    n.append(int(set[4]))
    n.append(int(set[9]))
    n.append(int(set[4]))
    n.append(int(set[3]))
    for i in range(0,15):
        if n[i] == 0:
            n[i] = 10
    if n[3] > n[0]:
        if n[6] > n[0]:
            if n[9] > n[0]:
                if n[12] > n[0]:
                    if n[0]+n[1]+n[2] == n[3]+n[4]+n[5]:
                        if n[0]+n[1]+n[2] == n[6]+n[7]+n[8]:
                            if n[0]+n[1]+n[2] == n[9]+n[10]+n[11]:
                                if n[0]+n[1]+n[2] == n[12]+n[13]+n[14]:
                                    S = ""
                                    for i in range(0,15):
                                        S = S + str(n[i])
                                    if len(S) == 16:
                                        if long(S) > max:
                                            max = long(S)
                                        print "Total:"+str(x)+" Set:"+S+" Max:"+str(max)
print "Answer:"+str(max)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s