42 lines
797 B
Python
42 lines
797 B
Python
import sys
|
|
from itertools import cycle
|
|
|
|
L = sys.stdin.read().splitlines()
|
|
N = L[2:]
|
|
|
|
M = []
|
|
for x in N:
|
|
(a, b) = x.split(" = ")
|
|
c = b.split(", ")
|
|
M.append( (a, c[0].replace("(", ""), c[1].replace(")", "")) )
|
|
|
|
S = 0
|
|
tri = []
|
|
for a, b, c in M:
|
|
for d, e, f in a.split():
|
|
if f == "A" : tri.append(a)
|
|
|
|
#print(tri)
|
|
|
|
for i in cycle(L[0]) :
|
|
for idx, t in enumerate(tri):
|
|
for a, b, c in M:
|
|
if a == t:
|
|
#print(t, a, b, c)
|
|
tri[idx] = b if i == "L" else c
|
|
break
|
|
S+=1
|
|
#print(tri)
|
|
fin = True
|
|
for a in tri:
|
|
for d, e, f in a.split():
|
|
#print(a, f)
|
|
if f == "Z":
|
|
fin = True and fin
|
|
else:
|
|
fin = False
|
|
if fin: break
|
|
|
|
print(S)
|
|
|