Advertisement
ahagelberg

Code puzzle solver

Mar 6th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
  2.  
  3.  
  4. def one_in_place(a, b, c):
  5.     return [
  6.         [{a},          digits - {b}, digits - {c}],
  7.         [digits - {a}, {b},          digits - {c}],
  8.         [digits - {a}, digits - {b}, {c}]
  9.     ]
  10.  
  11. def one_wrong_place(a, b, c):
  12.     remaining = digits - {a, b, c}
  13.     return [
  14.         [{b, c},    remaining, remaining],
  15.         [remaining, {a, c},    remaining],
  16.         [remaining, remaining, {a, b}]
  17.     ]
  18.  
  19. def two_wrong_place(a, b, c):
  20.     remaining = digits - {a, b, c}
  21.     return [
  22.         [{b, c}, {a, c}, remaining],
  23.         [{b, c}, remaining, {a, c}],
  24.         [remaining, {a, c}, {a, b}]
  25.     ]
  26.  
  27. def all_wrong(a, b, c):
  28.     remaining = digits - {a, b, c}
  29.     return [ [remaining, remaining, remaining] ]
  30.  
  31.  
  32. rules = [
  33.     one_in_place(6, 8, 2),
  34.     one_wrong_place(6, 1, 4),
  35.     two_wrong_place(2, 0, 6),
  36.     all_wrong(7, 3, 8),
  37.     one_wrong_place(7, 8, 0)
  38. ]
  39.  
  40.  
  41. for a in range(10):
  42.     for b in range(10):
  43.         for c in range(10):
  44.             match = True
  45.             for rule in rules:
  46.                 rule_match = False
  47.                 for set in rule:
  48.                     if a in set[0] and b in set[1] and c in set[2]:
  49.                         rule_match = True
  50.                 if not rule_match:
  51.                     match = False
  52.                     break
  53.             if match:
  54.                 print(f"Matching code found: {a}{b}{c}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement