Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import combinations, product
- def matches(inp, comp, num_exact_pos, num_wrong_pos):
- count_exact = sum([inp[i] == comp[i] for i in xrange(len(inp))])
- count_inexact = len(set(inp).intersection(set(comp))) - count_exact
- return count_exact == num_exact_pos and count_inexact == num_wrong_pos
- def matches_all(code, rules):
- return all([matches(code, y[0], y[1], y[2]) for y in rules])
- # rules are composed of a guessed value, the number of exact matches, and the number of matches in wrong places
- all_rules = [
- [[6,8,2], 1, 0],
- [[6,1,4], 0, 1],
- [[2,0,6], 0, 2],
- [[7,3,8], 0, 0],
- [[7,8,0], 0 ,1]
- ]
- # add numbering
- all_rules = [all_rules[i] + [i+1] for i in xrange(len(all_rules))]
- # examines all combinations of rules
- for i in xrange(len(all_rules)):
- for rules in combinations(all_rules, i+1):
- results = [code for code in product(range(10), repeat=len(rules[0][0])) if matches_all(code, rules)]
- rule_nums = [x[3] for x in rules]
- if len(results) == 0:
- print "rules" , rule_nums, "are contradictory"
- elif len(results) == 1:
- print "rules" , rule_nums, "are suficient,", "result =", "".join(map(str,results[0]))
- else:
- print "rules" , rule_nums, "are insuficient,", len(results), "matches found"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement