Guest User

Untitled

a guest
Jan 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. def product(*args, repeat=1):
  2. pools = [list(pool) for pool in args] * repeat
  3. result = [[]]
  4. for pool in pools:
  5. result = [x+[y] for x in result for y in pool]
  6. for prod in result:
  7. yield list(prod)
  8.  
  9. def sort_list(total_list):
  10. d0=list()
  11. d1=list()
  12. d2=list()
  13. d3=list()
  14. d4=list()
  15.  
  16. for x in total_list:
  17. mlist=list()
  18. alist = x
  19. n = 0
  20. while n<4:
  21. m = alist.count(alist[n])
  22. mlist.append(m)
  23. n = n + 1
  24. if max(mlist)==1:
  25. d0.append(alist)
  26. elif max(mlist)==2 and mlist.count(2)==2:
  27. d1.append(alist)
  28. elif max(mlist)==2 and mlist.count(2)>2:
  29. d2.append(alist)
  30. elif max(mlist)==3 :
  31. d3.append(alist)
  32. elif max(mlist)==4 :
  33. d4.append(alist)
  34.  
  35. total_list = d0+d1+d2+d3+d4
  36. possible = [''.join(p) for p in total_list]
  37. return total_list
  38.  
  39. def computeFeedback(code,guess):
  40. b = 0
  41. w = 0
  42. inCodeCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
  43. inGuessCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
  44. for i in range(0,4):
  45. if code[i] == guess[i]:
  46. b += 1
  47. inCodeCount[code[i]]+=1
  48. inGuessCount[guess[i]]+=1
  49. for ch in inCodeCount:
  50. w+=min(inCodeCount [ch], inGuessCount [ch])
  51. w-=b
  52. feedback = str(w)+'w'+str(b)+'b'
  53. return feedback
  54.  
  55. guesscount=0
  56. code=input()
  57. while guesscount<8:
  58. guesscount += 1
  59. if guesscount==1:
  60. guess='ABCD'
  61. print("My guess is:",guess)
  62. feedback=computeFeedback(code,guess)
  63. if feedback!='0w4b':
  64. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  65. overlap=list()
  66. for x in combinations:
  67. fb=computeFeedback(guess,x)
  68. overlap.append(x)
  69. if fb != feedback:
  70. overlap.pop()
  71. common=overlap
  72. overlap=list()
  73.  
  74. else:
  75. print("Game Over in", guesscount,"steps")
  76. break
  77. if guesscount==2:
  78. guess='BCDE'
  79. print("My guess is:",guess)
  80. feedback=computeFeedback(code,guess)
  81. if feedback!='0w4b':
  82. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  83. overlap=list()
  84. for x in combinations:
  85. fb=computeFeedback(guess,x)
  86. overlap.append(x)
  87. if fb != feedback:
  88. overlap.pop()
  89. common=[token for token in overlap if token in common]
  90. overlap=list()
  91.  
  92. else:
  93. print('Game Over in,', guesscount,'steps')
  94. break
  95. if guesscount==3:
  96. guess='CDEF'
  97. print("My guess is:",guess)
  98. feedback=computeFeedback(code,guess)
  99. if feedback!='0w4b':
  100. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  101. overlap=list()
  102. for x in combinations:
  103. fb=computeFeedback(guess,x)
  104. overlap.append(x)
  105. if fb != feedback:
  106. overlap.pop()
  107. common=[token for token in overlap if token in common]
  108.  
  109. overlap=list()
  110. else:
  111. print('Game Over in', guesscount,'steps')
  112. break
  113. if guesscount==4:
  114. if common[0]=="acfb".upper():
  115. guess="dcad".upper()
  116. if common[0]=="aebf".upper():
  117. guess="edfd".upper()
  118. if common[0]=='AEFB':
  119. guess='EACC'
  120. if common[0]=='AFBE':
  121. guess='BFCD'
  122. if common[0]=='BAFE':
  123. guess='EADC'
  124. if common[0]=='BEAF':
  125. guess='EDAE'
  126. if common[0]=='BEFA':
  127. guess='EEDA'
  128. if common[0]=='EABF':
  129. guess='FDFB'
  130. if common[0]=='AADB':
  131. guess='BABD'
  132. if common[0]=='ABAE':
  133. guess='BBCC'
  134. if common[0]=='AEAF':
  135. guess='CFFD'
  136. if common[0]=='CAFA':
  137. guess='FDFA'
  138. if common[0]=='AAEE':
  139. guess='DDDF'
  140. else:
  141. guess=common[0]
  142. print("My guess is:",guess)
  143. if len(common)>1:
  144. common.pop(0)
  145.  
  146. feedback=computeFeedback(code,guess)
  147. if feedback!='0w4b':
  148. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  149. overlap=list()
  150. for x in combinations:
  151. fb=computeFeedback(guess,x)
  152. overlap.append(x)
  153. if fb != feedback:
  154. overlap.pop()
  155. common=[token for token in overlap if token in common]
  156. overlap=list()
  157. else:
  158. print('Game Over in', guesscount,'steps')
  159. break
  160. else:
  161. print('Game Over in', guesscount,'steps')
  162. break
  163. if guesscount==5:
  164. guess=common[0]
  165. print("My guess is:",guess)
  166. if len(common)>1:
  167. common.pop(0)
  168.  
  169. feedback=computeFeedback(code,guess)
  170. if feedback!='0w4b':
  171. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  172. overlap=list()
  173. for x in combinations:
  174. fb=computeFeedback(guess,x)
  175. overlap.append(x)
  176. if fb != feedback:
  177. overlap.pop()
  178. common=[token for token in overlap if token in common]
  179.  
  180. overlap=list()
  181. else:
  182. print('Game Over in', guesscount,'steps')
  183. break
  184. else:
  185. print('Game Over in', guesscount,'steps')
  186. break
  187. if guesscount==6:
  188. guess=common[0]
  189. print("My guess is:",guess)
  190. if len(common)>1:
  191. common.pop(0)
  192.  
  193. feedback=computeFeedback(code,guess)
  194. if feedback!='0w4b':
  195. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  196. overlap=list()
  197. for x in combinations:
  198. fb=computeFeedback(guess,x)
  199. overlap.append(x)
  200. if fb != feedback:
  201. overlap.pop()
  202. common=[token for token in overlap if token in common]
  203.  
  204. overlap=list()
  205. else:
  206. print('Game Over in', guesscount,'steps')
  207. break
  208. else:
  209. print('Game Over in', guesscount,'steps')
  210. break
  211. if guesscount==7:
  212. guess=common[0]
  213. print("My guess is:",guess)
  214. if len(common)>1:
  215. common.pop(0)
  216. feedback=computeFeedback(code,guess)
  217. if feedback!='0w4b':
  218. combinations=sort_list([''.join(x) for x in product('ABCDEF',repeat=4)])
  219. overlap=list()
  220. for x in combinations:
  221. fb=computeFeedback(guess,x)
  222. overlap.append(x)
  223. if fb != feedback:
  224. overlap.pop()
  225. common=[token for token in overlap if token in common]
  226.  
  227. overlap=list()
  228. else:
  229. print('Game Over in', guesscount,'steps')
  230. break
  231. else:
  232. print('Game Over in', guesscount,'steps')
  233. break
  234. if guesscount==8:
  235. print('Failure')
  236.  
  237. class CodeBreaker:
  238. def __init__(self):
  239.  
  240.  
  241.  
  242. def makeGuess(self):
  243.  
  244. return guess
  245.  
  246. def getFeedback(self, feedbackStr):
  247.  
  248. from mastermind import CodeBreaker
  249.  
  250. def computeFeedback(code,guess):
  251. # Given a code and guess computes the feedback string
  252.  
  253. b = 0
  254. w = 0
  255. inCodeCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
  256. inGuessCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
  257. for i in range(0,4):
  258. if code[i] == guess [i]:
  259. b += 1
  260. inCodeCount[code[i]] += 1
  261. inGuessCount[guess[i]] += 1
  262. for ch in inCodeCount:
  263. w += min(inCodeCount [ch], inGuessCount [ch])
  264. w -= b
  265. feedback = str(w)+'w'+str(b)+'b'
  266. return feedback
  267.  
  268. code = input()
  269.  
  270. while (code != None):
  271. guesscount = 0
  272. myBreaker = CodeBreaker()
  273. while (guesscount < 8):
  274. guess = myBreaker.makeGuess()
  275. print("My guess is:",guess)
  276. guesscount += 1
  277. feedback = computeFeedback(code, guess)
  278. print(feedback)
  279. if feedback == "0w4b":
  280. print("Game Over in", guesscount,"steps")
  281. break
  282. myBreaker.getFeedback(feedback)
  283. if guesscount == 8:
  284. print("Failed to find the solution in 8 steps")
  285. code = input()
  286.  
  287. class CodeBreaker:
  288. def __init__(self):
  289. self.guess_count = 0
  290. self.guess = 'ABCD'
  291. self.common = []
  292.  
  293. def makeGuess(self):
  294. return self.guess
  295.  
  296. def getFeedback(self, feedback):
  297. self.guess_count += 1
  298. if self.guess_count == 1:
  299. # ... your code here ...
  300. self.guess = 'BCDE'
  301. elif self.guess_count == 2:
  302. # ... your code here ...
  303. self.guess = 'CDEF'
  304. elif self.guess_count == 3:
  305. # ... your code here ...
  306. # ... and so on ...
  307.  
  308. from itertools import product
  309. from random import choice
  310. from copy import copy
  311.  
  312. class CodeBreaker(object):
  313. all_codes = [''.join(c) for c in product(*['ABCDEF'] * 4)]
  314.  
  315. def __init__(self):
  316. self.codes = copy(self.all_codes)
  317. self.guess = 'AABB' # Best first guess!
  318.  
  319. def makeGuess(self):
  320. return self.guess
  321.  
  322. def getFeedback(self, feedback):
  323. self.codes = [c for c in self.codes if computeFeedback(c, self.guess) == feedback]
  324. self.guess = choice(self.codes)
  325.  
  326. def getFeedback(self, feedback):
  327. self.codes = [c for c in self.codes if computeFeedback(c, self.guess) == feedback]
  328. def key(g): return max(Counter(computeFeedback(c, g) for c in self.codes).values())
  329. self.guess = min(self.codes, key = key)
  330.  
  331. def getFeedback(self, feedback):
  332. self.codes = [c for c in self.codes if computeFeedback(c, self.guess) == feedback]
  333. if len(self.codes) == 1:
  334. self.guess = self.codes[0]
  335. else:
  336. def key(g): return max(Counter(computeFeedback(c, g) for c in self.codes).values())
  337. self.guess = min(self.all_codes, key = key)
  338.  
  339. from collections import Counter
  340.  
  341. def computeFeedback(code, guess):
  342. """
  343. Return the string `{white}w{black}b` where `black` is a count of the
  344. characters in `guess` that appear at the same position in `code`
  345. and `white` is a count of the characters in `guess` that appear at
  346. a different position in `code`.
  347.  
  348. >>> computeFeedback('ABCD', 'ACAD')
  349. '1w2b'
  350. >>> computeFeedback('ABAB', 'AABB')
  351. '2w2b'
  352. >>> computeFeedback('ABCD', 'DCBA')
  353. '4w0b'
  354. """
  355. w = sum((Counter(code) & Counter(guess)).values())
  356. b = sum(c == g for c, g in zip(code, guess))
  357. return '{}w{}b'.format(w - b, b)
Add Comment
Please, Sign In to add comment