import time import sys import random import math filein=open("in_sec18_19.txt","r") fileout=open("out_sec18_19.txt","w") g=filein.readline().rstrip("\n").split(" ") h=filein.readline().rstrip("\n").split(" ") count=0 t=0 p,d,r,l=[],[],[],[] for i in range(len(h)): m=filein.readline().rstrip("\n").split(" ") for k in range(12): if(h[k]=='0'): count=k h[k]=m[count] count=0 tell={g[0]:h[0],g[1]:h[1],g[2]:h[2],g[3]:h[3],g[4]:h[4],g[5]:h[5],g[6]:h[6],g[7]:h[7],g[8]:h[8],g[9]:h[9],g[10]:h[10],g[11]:h[11]} h.sort() t=h.count('1')+h.count('2') m=0 count=1 s=0 def cycle3(l): if(l==11 or l==10 or l==15): return(0) def cycle4(l): if(l==11 or l==12 or l==19): return(0) def cycle5(l): if(l==15 or l==17): return(0) def find_key(dic, val): """return the key of dictionary dic given the value""" return [k for k, v in tell.iteritems() if v == val][0] for i in h: k= find_key(tell,i) r.append(k) del tell[k] p.append(int(i)) p.sort() for i in range(t+1): d.append(p[i]) '''if(len(p)>=t): for i in range(720): random.shuffle(p)''' while(len(d)<=8): for y in range(math.factorial(t)): random.shuffle(d) for y in range(5): m+=d[y] f=cycle5(m) if(f==0): t+=1 d.sort() d.remove(d[len(d)-1]) for i in d: fileout.write(r[s]+"\n") print r[s] s+=1 m=0 j=math.factorial(t-1) for y in range(j): random.shuffle(d) for u in range(4): m+=d[u] s=cycle4(m) if(s==0): break m=0 j=math.factorial(t-2) for y in range(j): random.shuffle(d) for u in range(3): m+=d[u] o=cycle3(m) if(o==0): break m=0 t=t+1 d.append(p[t])