1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

def options(A, start=None, best=1): #start and best are only there so that I can restart the program at a later point
max=1
if(start==None):
current=[0]*len(A)
else:
current=start
for B in A:
if max<len(A[B]):
max=len(A[B])
while(next_permutation(A, current)):
print(current)
c=(score(A, current, max))
if(c[0]<best):
output(c, 'w', current)
best=c[0]
print("Best:"+str(best))
elif(c[0]==best):
output(c, 'a', current)
print("1")
def score(A, current, max):
options=[{} for i in xrange(max)]
for i, B in zip(xrange(len(current)), A):
p=get_permutation(current[i], len(A[B]))
for j,num in zip(xrange(len(A[B])),p):
if(options[num1].has_key(A[B][j])):
options[num1][A[B][j]].append(B)
else:
options[num1][A[B][j]]=[B]
ans=0
for group in options:
if(group>4):
ans+=2
for subject in group:
if(len(group[subject])<4):
ans+=4len(group[subject])
return(ans, options)
def get_permutation(i, N):
p0, p=[k for k in xrange(1, N+1)], []
for j in xrange(N1, 0, 1):
j_f=reduce(lambda x, y: x*y, [k for k in xrange(1, j+1)])
c=i/j_f
i%=j_f
p.append(p0[c])
p0=p0[:c]+p0[c+1:]
p.append(p0[0])
return(p)
def next_permutation(A, p):
for i, B in zip(xrange(len(p)), A):
fact=reduce(lambda x, y: x*y, [k for k in xrange(1, len(A[B])+1)])
p[i]+=1
if(p[i]==fact):
p[i]=0
else:
return(True)
return(False)
def output(option, mode, current):
fout=open("ans.txt", mode)
fout.write("=============================\n")
fout.write("Score: "+str(option[0])+"\n[")
for i in current:
fout.write(str(i)+", ")
fout.write("]\n")
for i,group in zip(xrange(len(option[1])), option[1]):
fout.write("Group "+str(i)+"\n")
for subject in group:
fout.write(subject+": ")
for name in group[subject]:
fout.write(name+", ")
fout.write("\n")
fout.close()
values={"0" : ("A","B","C",),
"1" : ("D","C","E","F",),
"2" : ("D","E","G","F",),
"3" : ("B","C","E","F",),
"4" : ("A","D","H","E",),
"5" : ("A","D","I",),
"6" : ("A","D","B",),
"7" : ("A","D","B","H",),
"8" : ("A","D","B","I",),
"9" : ("A","D","J",),
"10" : ("D","B","J",),
"11" : ("A","D","B",),
"12" : ("A","D","I","G",),
"13" : ("D","C","H",),
"14" : ("B","C","F",),
"15" : ("A","D","B",),
"16" : ("D","B","C","E",),
"17" : ("A","D","I",),
"18" : ("A","D","B",),
"19" : ("A","B","E","F",),
"20" : ("A","G","F","J",),
"21" : ("A","E","G","F",),
"22" : ("A","E","G","J",),
"23" : ("A","D","B",),
"24" : ("D","B","C",),
"25" : ("D","C","I",),
"26" : ("A","D","B",),
"27" : ("A","D","I","J",),
"28" : ("A","D","J",),
"29" : ("A","C","I",),
"30" : ("A","D","E",),
"31" : ("A","H","J",),
}
options(values)
 