判断推理のカードの問題をPythonで解いてみた。(4)

 判断推理のカードの問題Pythonで解いてみました。(^_^;

 1から9までの数字を一つずつ書いた9枚のカードがある。A~Cの3人がこの中から任意の3枚ずつとったところ、
Aの取ったカードに書かれていた数の合計は13でその中には7が入っていた。
Bの取ったカードに書かれていた数の合計は12で、その中には4が入っていた。
Cの取ったカードに書かれていた数の中に入っていた数は、次のうちではどれか。
1.1、 2.2、 3.5、 4.6、 5.8

 組合せで回した方が速いですが、あえて順列で回してみました。最近はPCが速くなったので、あまり差はないです。(^_^;

● Cards4.py

# coding: UTF-8
# Cards4.py

import itertools
from time import time

# 確実にいえる選択肢を得る
def getAns(cho,lbl='12345'):
    return ','.join([lbl[i] for i in range(len(cho)) if cho[i]])

def main():
    tm = time()  # Timer Start
    choices = [True]*5
    Cards = range(1,10)
    for p in itertools.permutations(Cards):
        a,b,c = p[:3],p[3:6],p[6:]
        if not a[0]< a[1]< a[2]: continue
        if not b[0]< b[1]< b[2]: continue
        if not c[0]< c[1]< c[2]: continue
        if not sum(a)==13: continue     # 条件A-1
        if not 7 in a: continue         # 条件A-2
        if not sum(b)==12: continue     # 条件B-1
        if not 4 in b: continue         # 条件B-2
        pass # チェックを潜り抜けたものを表示
        print('A:',a)
        print('B:',b)
        print('C:',c)
        pass # 選択肢のチェック
        choices[0] &= (1 in c)
        choices[1] &= (2 in c)
        choices[2] &= (5 in c)
        choices[3] &= (6 in c)
        choices[4] &= (8 in c)

    print("∴%s"%getAns(choices))
    print("Runtime : %.3f [sec]"%(time()-tm))   # Timer Stop & Disp

if __name__ == '__main__':
    main()

●実行結果

A: (1, 5, 7)
B: (2, 4, 6)
C: (3, 8, 9)
∴5
Runtime : 0.531 [sec]

※参考URL
判断推理のカードの問題をPythonで解いてみた。 - rscのブログ
判断推理のカードの問題をPythonで解いてみた。(2) - rscのブログ
判断推理のカードの問題をPythonで解いてみた。(3) - rscのブログ
判断推理のカードの問題をPythonで解いてみた。(5) - rscのブログ