Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

51 Zeilen
1.3KB

  1. import collections
  2. import itertools
  3. import re
  4. import sys
  5. txt = '''90342 ;2 correct
  6. 70794 ;0 correct
  7. 39458 ;2 correct
  8. 34109 ;1 correct
  9. 51545 ;2 correct
  10. 12531 ;1 correct
  11. ''' # 39542 is unique
  12. txt = sys.stdin.read()
  13. exclusions = collections.defaultdict(set)
  14. clues = []
  15. for string, n in re.findall(r'(\d+) ;(\d)', txt):
  16. choices = set(enumerate(string))
  17. combos = [frozenset(cs) for cs in itertools.combinations(choices, int(n))]
  18. for combo in combos:
  19. exclusions[combo] |= (choices - combo)
  20. if int(n):
  21. clues.append(combos)
  22. for pos in range(len(string)):
  23. choices = {(pos, char) for char in '0123456789'}
  24. for choice in choices:
  25. exclusions[frozenset({choice})] |= (choices - {choice})
  26. def solve(clues_left, known=frozenset()):
  27. if not clues_left:
  28. avail = {a for gr in exclusions for a in gr}
  29. avail -= {v for k, vs in exclusions.items() if k <= known for v in vs}
  30. yield ''.join(c for _, c in sorted(avail))
  31. else:
  32. choices = min(clues_left, key=len)
  33. for choice in choices:
  34. chosen = known | choice
  35. bads = {v for k, vs in exclusions.items() if k <= chosen for v in vs}
  36. mods = [[c for c in cl if not c & bads] for cl in clues_left if cl != choices]
  37. yield from solve(mods, chosen)
  38. solution = next(solve(clues))
  39. print(solution)