Guest User

Untitled

a guest
Dec 9th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. import itertools
  2.  
  3. def encode(state):
  4.   s = ''
  5.   for l in state:
  6.     if l:
  7.       s = s + l[0]
  8.     else:
  9.       s = s + '_'
  10.   return s
  11.  
  12. def pretty(state):
  13.   return '|'.join([''.join(slot) or '_' for slot in state])
  14.  
  15. def mutate(state, dirs):
  16.   fr, to = int(dirs[0])-1, int(dirs[1])-1
  17.   l = state[fr].pop(0)
  18.   state[to].insert(0,l)
  19.   return
  20.  
  21. def iteration(state, decider):
  22.   dirs = decider[encode(state)]
  23.   mutate(state, decider[encode(state)])
  24.   return
  25.  
  26. slots = ["","Q","A","K","QA","QK","KA","KQ","AQ","AK","QAK","QKA","KQA","KAQ","AQK","AKQ"]
  27.  
  28. def final(state):
  29.   if ''.join(state[0]) == "AKQ" and len(state[1])==0 and len(state[2])==0:
  30.     return True
  31.   else:
  32.     return False
  33.  
  34. def gen_states():
  35.   list_slots = [list(x) for x in slots]
  36.   for L in list_slots:
  37.     ls = set(itertools.chain(*L))
  38.     for M in list_slots:
  39.       ms = set(itertools.chain(*M))
  40.       if ls.intersection(ms):
  41.         continue
  42.       for R in list_slots:
  43.         rs = set(itertools.chain(*R))
  44.         if ls.intersection(rs) or ms.intersection(rs):
  45.           continue
  46.         if len(ls | ms | rs) != 3:
  47.           continue
  48.         yield([list(L),list(M),list(R)])
  49.  
  50.  
  51. def loop(state, decider):
  52.   known = set()
  53.   chain = ''
  54.   while True:
  55.     chain += pretty(state)
  56.     if final(state):
  57.       return True, chain
  58.     if pretty(state) in known:
  59.       return False, chain  # loop
  60.     known.add(pretty(state))
  61.     iteration(state, decider)
  62.     chain += '  -->  '
  63.   return  # shouldn't reach
  64.  
  65. def test_decider(decider):
  66.   states = list(gen_states())
  67.   for s in states:
  68.     res, chain = loop(s, decider)
  69.     if not res:
  70.       print "FAILED:"
  71.     print chain
  72.     if not res:
  73.       return
  74.  
  75. decider = {
  76.   "Q__": "12",
  77.   "K__": "12",
  78.   "A__": "12",
  79.   "_Q_": "12",
  80.   "_K_": "12",
  81.   "_A_": "12",
  82.   "__Q": "12",
  83.   "__K": "12",
  84.   "__A": "12",
  85.  
  86.   "KA_": "12",
  87.   "K_A": "12",
  88.   "_KA": "12",
  89.   "AK_": "12",
  90.   "A_K": "12",
  91.   "_AK": "12",
  92.  
  93.   "AQ_": "12",
  94.   "_QA": "12",
  95.   "Q_A": "12",
  96.   "QA_": "12",
  97.   "_AQ": "12",
  98.   "A_Q": "12",
  99.  
  100.   "KQ_": "12",
  101.   "_QK": "12",
  102.   "Q_K": "12",
  103.   "QK_": "12",
  104.   "_KQ": "12",
  105.   "K_Q": "12",
  106.  
  107.   "QKA": "12",
  108.   "QAK": "12",
  109.  
  110.   "KQA": "12",
  111.   "KAQ": "12",
  112.   "AQK": "12",
  113.   "AKQ": "12",
  114. }
  115.  
  116. test_decider(decider)
Advertisement
Add Comment
Please, Sign In to add comment