Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

32 lines
816B

  1. import math
  2. import collections
  3. import itertools
  4. import re
  5. text = open(0).read()
  6. head, tail = text.split('\n\n')
  7. mapping = {}
  8. for line in tail.splitlines():
  9. aa, bb, cc = re.findall(r'[\dA-Z]{3}', line)
  10. mapping[aa] = [bb, cc]
  11. pos = 'AAA'
  12. steps = itertools.cycle(map('LR'.index, head))
  13. for ans1 in itertools.count(1):
  14. pos = mapping[pos][next(steps)]
  15. if pos == 'ZZZ':
  16. break
  17. print(ans1)
  18. state = [p for p in mapping if p.endswith('A')]
  19. seen = collections.defaultdict(collections.Counter)
  20. steps = itertools.cycle(enumerate(map('LR'.index, head)))
  21. for _ in range(100_000):
  22. i, step = next(steps)
  23. state = [mapping[pos][step] for pos in state]
  24. for j, pos in enumerate(state):
  25. seen[j][i, pos] += 1
  26. ns = [sum(v > 1 for v in c.values()) for c in seen.values()]
  27. print(math.lcm(*ns))