08_desertmap.py 523 B

123456789101112131415
  1. #!/usr/bin/env python3
  2. import sys
  3. from itertools import cycle, islice
  4. from math import lcm
  5. def follow(instructions, graph, cur, end):
  6. for step, inst in enumerate(cycle(instructions)):
  7. if cur.endswith(end):
  8. return step
  9. cur = graph[cur][inst]
  10. inst = ['LR'.index(c) for c in next(sys.stdin).rstrip()]
  11. graph = {l[:3]: (l[7:10], l[12:15]) for l in islice(sys.stdin, 1, None)}
  12. print(follow(inst, graph, 'AAA', 'ZZZ'))
  13. print(lcm(*(follow(inst, graph, n, 'Z') for n in graph if n.endswith('A'))))