23 lines
499 B
Python
23 lines
499 B
Python
|
import sys
|
||
|
from itertools import cycle
|
||
|
from math import lcm
|
||
|
|
||
|
I, _, *P = [l for l in sys.stdin.read().splitlines()]
|
||
|
Q = [l.split(' = ') for l in P ]
|
||
|
P = {k:v[1:-1].split(", ") for [k,v] in Q }
|
||
|
|
||
|
G = [p for p in P.keys() if p[2] == 'A' ]
|
||
|
R = []
|
||
|
for n,g in enumerate(G):
|
||
|
print(n+1, g, '', end='')
|
||
|
C = g
|
||
|
N = 0
|
||
|
for i in cycle(I):
|
||
|
if C[2] == 'Z':
|
||
|
break
|
||
|
C = P[C][i == 'R']
|
||
|
N += 1
|
||
|
print(N)
|
||
|
R.append(N)
|
||
|
print('steps: sum:', sum(R), 'lcm:', lcm(*R))
|