Skip to content

Instantly share code, notes, and snippets.

@jeek
Created September 28, 2015 03:05
Show Gist options
  • Save jeek/9fbc89f1ed48ac8c013c to your computer and use it in GitHub Desktop.
Save jeek/9fbc89f1ed48ac8c013c to your computer and use it in GitHub Desktop.
import sys
from itertools import permutations
class Memoize:
def __init__(self, f):
self.f = f
self.memo = {}
def __call__(self, *args):
if not str(args) in self.memo:
self.memo[str(args)] = self.f(*args)
return self.memo[str(args)]
target = 12345 + 23451 + 34512 + 45123 + 51234
singlequeue = [int("".join(i)) for i in permutations("12345")]
biglist = dict()
for i in xrange(1, 6):
biglist[i] = dict()
for i in singlequeue:
for j in xrange(1, 6):
biglist[j][i] = set()
def mean(alist):
return sum(alist) / len(alist)
def stddev(alist):
return (mean([(i * 1.0 - mean(alist)) ** 2 for i in alist]) ** .5)
# abcde
# fghij
# klmno
# pqrst
# uvwxy
if __name__ == "__main__":
queue = []
for a in singlequeue:
for b in [bb for bb in singlequeue if bb > a]:
for c in [cc for cc in singlequeue if cc > b]:
for d in [dd for dd in singlequeue if dd > c]:
for e in [ee for ee in singlequeue if ee > d]:
if a + b + c + d + e == target:
for f in permutations([a, b, c, d, e]):
queue.append(f)
biglist[1][f[0]].add(f)
biglist[2][f[1]].add(f)
biglist[3][f[2]].add(f)
biglist[4][f[3]].add(f)
biglist[5][f[4]].add(f)
queue = sorted(queue, key=stddev)
for [a, g, m, s, y] in queue:
for [e, i, mm, q, u] in biglist[3][m]:
if mm == m and len(set([a,e,g,i,m,q,s,u,y])) == 9:
sys.stderr.write(str([a,g,m,s,y]) + " - " + str([e,i,m,q,u]) + "\n")
for [aa, b, c, d, ee] in sorted(biglist[1][a] & biglist[5][e]):
if aa == a and ee == e and len(set([a,b,c,d,e,g,i,m,q,s,u,y])) == 12:
sys.stderr.write(" " + " ".join([str(zzz) for zzz in [a,b,c,d,e]]) + "\n")
for [aa,f,k,p,uu] in (biglist[1][a] & biglist[5][u]):
if aa == a and uu == u and len(set([a,b,c,d,e,f,g,i,k,m,p,q,s,u,y])) == 15:
for [ff, gg, h, ii, j] in (biglist[1][f] & biglist[2][g] & biglist[4][i]):
if ff == f and gg == g and ii == i and len(set([a,b,c,d,e,f,g,h,i,j,k,m,p,q,s,u,y])) == 17:
for [bb, gg, l, qq, v] in (biglist[1][b] & biglist[2][g] & biglist[4][q]):
if bb == b and gg == g and qq == q and len(set([a,b,c,d,e,f,g,h,i,j,k,l,m,p,q,s,u,v,y])) == 19:
for [kk,ll,mm,n,o] in (biglist[1][k] & biglist[2][l] & biglist[3][m]):
if kk == k and ll == l and mm == m and len(set([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,s,u,v,y])) == 21:
# sys.stderr.write(" " + " ".join([str(zzz) for zzz in [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o]]) + "\r")
for [cc,hh,mm,r,w] in (biglist[1][c] & biglist[2][h] & biglist[3][m]):
if cc == c and hh == h and mm == m and len(set([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,u,v,w,y])) == 23:
for [pp,qq,rr,ss,t] in (biglist[1][p] & biglist[2][q] & biglist[3][r] & biglist[4][s]):
if pp == p and qq == q and rr == r and ss == s and len(set([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,y])) == 24:
if [e,j,o,t,y] in queue:
for [dd,ii,nn,ss,x] in (biglist[1][d] & biglist[2][i] & biglist[3][n] & biglist[4][s]):
if dd == d and ii == i and nn == n and ss == s and len(set([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y])) == 25:
if [u,v,w,x,y] in queue:
print a,b,c,d,e
print f,g,h,i,j
print k,l,m,n,o
print p,q,r,s,t
print u,v,w,x,y
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment