Created
April 23, 2016 07:30
-
-
Save elnikkis/f580db5667fe6b12138f8b8325e7e530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
''' | |
論理パズルを解く | |
https://twitter.com/hikitashisan/status/723557720930951168 | |
''' | |
import itertools | |
def constraints(params): | |
t1 = lambda A, B, C, D, E, F, G, H, I, J: A*100 + B*10 + C + D*100 + E*10 + F == G*1000 + H*100 + I*10 + J | |
t2 = lambda p: len(p) == len(set(p)) | |
ta = lambda p: p[3] % 2 == 0 | |
tb = lambda p: p[5] == 8 | |
tc = lambda p: p[1] % 2 == 1 | |
td = lambda p: p[0] % 2 == 0 | |
te = lambda p: p[2] > p[3] | |
tf = lambda p: p[4] % 2 == 1 | |
tg = lambda p: p[9] % 2 == 0 | |
tj = lambda p: p[8] % 2 == 1 | |
t = [ta, tb, tc, td, te, tf, tg, lambda p: True, lambda p: True, tj] | |
# define method | |
l_and = lambda l1, l2: [i1 and i2 for i1, i2 in zip(l1, l2)] | |
l_or = lambda l1, l2: [i1 or i2 for i1, i2 in zip(l1, l2)] | |
is_even = lambda l: [True if i % 2 == 0 else False for i in l] | |
l_lie = lambda ft, even: [i1 if i2 else not i1 for i1, i2 in zip(ft, even)] | |
f = lambda funcs, p: [func(p) for func in funcs] | |
cond = l_lie(is_even(params), f(t, params)) | |
cond[7] = True | |
cond[8] = True | |
return t1(*params), t2(params), cond | |
def solve(): | |
'''解を探す | |
>>> solve() | |
(8, 7, 6, 4, 2, 9, 1, 3, 0, 5) | |
''' | |
for p in itertools.permutations(range(10), 6): | |
s = p[0]*100 + p[1]*10 + p[2] + p[3]*100 + p[4]*10 + p[5] | |
params = p + (s // 1000, (s // 100) % 10, (s // 10) % 10, s % 10) | |
at1, at2, cond = constraints(params) | |
if all((at1, at2, all(cond), params[0] != 0, params[3] != 0, params[6] != 0)): | |
return params | |
if __name__ == '__main__': | |
t = solve() | |
print(t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment