fork(21) download
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. def only_reserve_possible_answers(guess, bull, cow, al):
  5. anses = len(al)
  6. for k in xrange(anses - 1, -1, -1):
  7. if sum(1 for i, j in zip(al[k], guess)if i != j and j in al[k]) != cow \
  8. or sum(1 for i, j in zip(al[k], guess) if i == j) != bull:
  9. del al[k]
  10. return al
  11. def main():
  12. answer_candidates = [str(i) + str(j) + str(k) + str(l) \
  13. for i in xrange(0, 10) for j in xrange(0, 10) \
  14. for k in xrange(0, 10) for l in xrange(0, 10) \
  15. if len(set(str(i) + str(j) + str(k) + str(l))) == 4]
  16. counter = 1
  17. while True:
  18. guess = answer_candidates[0]
  19. print guess
  20. sys.stdout.flush()
  21. score = sys.stdin.readline().strip()
  22. bulls, cows = int(score[0]), int(score[2])
  23. if bulls == 4:
  24. break
  25. answer_candidates = only_reserve_possible_answers(
  26. guess, bulls, cows, answer_candidates)
  27. counter += 1
  28. print 'tried times:', counter
  29. if __name__ == '__main__':
  30. main()
Success #stdin #stdout 0.12s 10872KB
stdin
0a3b
3a0b
2a0b
4a0b
stdout
0123
1034
1035
1234
tried times: 4