覆面算

全く時間がないくせに、ニューロサイエンス特論で「インテリジェンスに問題を解くんだよ」という例で挙げられてた覆面算が妙に気になってしかたがない。
なので、プログラムを書いてみた。
所要時間5分ほどで書いた思いつきプログラム。
どんだけ力技やねん、と思わず言ってしまいそうなものが出来上がった。
しかし、一応問題は解ける。
[これはひどい]ね。
計算機様々、ということで。

# -*- encoding : utf-8 -*-

#  SEND
# +MORE
# ------
# MONEY

import sys

# リストの中に探索対象が含まれるかを調べる
def check(target, numList):
    for num in numList:
        if target == num:
            return True
    return False

def cal(S,E,N,D,M,O,R,Y):
    if (S*1000+E*100+N*10+D)+(M*1000+O*100+R*10+E) == (M*10000+O*1000+N*100+E*10+Y):
        return True
    return False

numList = []
for S in range(1,10):
    numList.append(S)
    for E in range(10):
        if check(E, numList) == True:
            continue
        else:
            numList.append(E)
        for N in range(10):
            if check(N, numList) == True:
                continue
            else:
                numList.append(N)
            for D in range(10):
                if check(D, numList) == True:
                    continue
                else:
                    numList.append(D)
                for M in range(1,10):
                    if check(M, numList) == True:
                        continue
                    else:
                        numList.append(M)
                    for O in range(10):
                        if check(O, numList) == True:
                            continue
                        else:
                            numList.append(O)
                        for R in range(10):
                            if check(R, numList) == True:
                                continue
                            else:
                                numList.append(R)
                            for Y in range(10):
                                if check(Y, numList) == True:
                                    continue
                                else:
                                    numList.append(Y)
                                        

                                if cal(S,E,N,D,M,O,R,Y) == True:
                                    print " SEND"
                                    print "+MORE"
                                    print "-----"
                                    print "MONEY"
                                    print 
                                    print " ",S,E,N,D
                                    print "+",M,O,R,E
                                    print "-------------"
                                    print M,O,N,E,Y
                                    sys.exit()
                                numList.remove(Y)
                            numList.remove(R)
                        numList.remove(O)
                    numList.remove(M)
                numList.remove(D)
            numList.remove(N)
        numList.remove(E)
    numList.remove(S)

結果。
一応正解。
sys.exit()で解が見つかった時点で終了しているので、これ以外の解があるかは知らない、という適当メソッド。
汚すぐるコードだ。

E:\study\program\Python>python test2.py
 SEND
+MORE
-----
MONEY

  9 5 6 7
+ 1 0 8 5
-------------
1 0 6 5 2

また後で直すかもしれない。