Created
March 7, 2015 21:50
-
-
Save ealmansi/cbd596717d4fed58128a to your computer and use it in GitHub Desktop.
5 houses problem
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from itertools import permutations | |
C = ["white", "blue", "yellow", "green", "red"] # colors | |
G = ["ps3", "xbox-one", "ps4", "pc", "xbox-360"] # game | |
P = ["galaxys5", "mslumia", "galaxys6", "iphone6", "iphone5"] # phone | |
N = ["brit", "american", "swedish", "german", "french"] # nationality | |
B = ["coca-cola", "coffee", "water", "beer", "pepsi"] # beverage | |
for pc in permutations(C): | |
cs = {v:i for i,v in enumerate(pc)} | |
if cs["red"] != cs["white"] - 1: continue # 4 | |
for pg in permutations(G): | |
gs = {v:i for i,v in enumerate(pg)} | |
if cs["green"] != gs["xbox-one"]: continue # 7 | |
for pp in permutations(P): | |
ps = {v:i for i,v in enumerate(pp)} | |
if gs["pc"] != ps["mslumia"]: continue # 6 | |
if gs["ps4"] != ps["iphone6"] - 1 and gs["ps4"] != ps["iphone6"] + 1: continue # 10 | |
if ps["galaxys6"] != gs["xbox-one"] - 1 and ps["galaxys6"] != gs["xbox-one"] + 1: continue # 11 | |
for pn in permutations(N): | |
ns = {v:i for i,v in enumerate(pn)} | |
if ns["brit"] != cs["blue"]: continue # 1 | |
if ns["french"] != ps["galaxys5"]: continue # 2 | |
if ns["american"] != 0: continue # 9 | |
if ns["swedish"] != gs["xbox-360"]: continue # 13 | |
if ns["american"] != cs["yellow"] - 1 and ns["american"] != cs["yellow"] + 1: continue # 14 | |
for pb in permutations(B): | |
bs = {v:i for i,v in enumerate(pb)} | |
if ns["german"] != bs["coca-cola"]: continue # 3 | |
if cs["red"] != bs["coffee"]: continue # 5 | |
if bs["beer"] != 2: continue # 8 | |
if gs["ps3"] != bs["beer"]: continue # 12 | |
if gs["ps4"] != bs["pepsi"] - 1 and gs["ps4"] != bs["pepsi"] + 1: continue # 15 | |
print cs, "\n", gs, "\n", ps, "\n", ns, "\n", bs, "\n--------------------------\n", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment