网上流传的“2018年刑侦科推理试题”的暴力破解程序
试题如上图。暴力破解的思想就是生成所有结果集,验证结果集中每个结果针对10道题是否正确。当10道题均正确表示找到了正确答案。
代码如下:
1 #!/bin/bash 2 # -*- coding: utf-8 -*- 3 4 import itertools,time 5 from collections import Counter 6 7 rs=['A','B','C','D'] 8 9 allRs=itertools.product(*([rs]*10)) 10 11 #问题函数列表 12 fs=[] 13 14 def q1(r): 15 return True 16 17 def q2(r): 18 return {'C':'A','D':'B','A':'C','B':'D'}[r[4]]==r[1] 19 20 def q3(r): 21 #return True 22 expectResult=r[{'A':3,'B':6,'C':2,'D':4}[r[2]]-1] 23 return list({3:r[2],6:r[5],2:r[1],4:r[3]}.values()).count(expectResult)==1 24 25 26 def q4(r): 27 tq1,tq2={'A':(1,5),'B':(2,7),'C':(1,9),'D':(6,10)}[r[3]] 28 return r[tq1-1]==r[tq2-1] 29 30 def q5(r): 31 return r[{'A':8,'B':4,'C':9,'D':7}[r[4]]-1]==r[4] 32 33 def q6(r): 34 tq1,tq2={'A':(2,4),'B':(1,6),'C':(3,10),'D':(5,9)}[r[5]] 35 return r[tq1-1]==r[tq2-1] and r[7]==r[tq1-1] 36 37 def q7(r): 38 expectResult={'A':'C','B':'B','C':'A','D':'D'}[r[6]] 39 tmpr=dict(Counter(r)) 40 tmpd={} 41 tmpd['A']=tmpr.get('A',0) 42 tmpd['B']=tmpr.get('B',0) 43 tmpd['C']=tmpr.get('C',0) 44 tmpd['D']=tmpr.get('D',0) 45 for k,v in tmpd.items(): 46 if v<tmpd.get(expectResult,0): 47 return False 48 return True 49 50 def q8(r): 51 tmpr=ord(r[{'A':7,'B':5,'C':2,'D':10}[r[7]]-1])-ord(r[0]) 52 return tmpr!=-1 and tmpr!=1 53 54 def q9(r): 55 return (r[0]==r[5])^((r[{'A':6,'B':10,'C':2,'D':9}[r[8]]-1])==r[4]) 56 57 def q10(r): 58 tmpr=dict(Counter(r)) 59 tmpl=list(tmpr.values()) 60 tmpl.sort() 61 return tmpl[-1]-tmpl[0]=={'A':3,'B':2,'C':4,'D':14}[r[9]] 62 63 fs.append(q1) 64 fs.append(q2) 65 fs.append(q3) 66 fs.append(q4) 67 fs.append(q5) 68 fs.append(q6) 69 fs.append(q7) 70 fs.append(q8) 71 fs.append(q9) 72 fs.append(q10) 73 74 foundedResult=[] 75 76 count=0 77 total=4**10 78 for r in allRs: 79 count+=1 80 if count%(int(total/100))==0 or count==total: 81 print('#'*int(count/total*100)+'-'*(100-int(count/total*100)),end='\r') 82 passed=True 83 for f in fs: 84 tmpfr=f(r) 85 if not tmpfr: 86 passed=False 87 break 88 if passed: 89 foundedResult.append(r) 90 91 print() 92 for r in foundedResult: 93 for k,v in enumerate(r): 94 print(f'第{k+1}题:{v}') 95 print()
啦啦啦!!!