You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
704 B
28 lines
704 B
from math import lcm
|
|
|
|
input = open("input.txt", 'r').read().splitlines()
|
|
instructions = input[0]
|
|
nodes = {l.split()[0]: (l.split()[2][1:-1], l.split()[3][:-1]) for l in input[2:]}
|
|
|
|
steps = 0
|
|
node = 'AAA'
|
|
|
|
while node != 'ZZZ':
|
|
node = nodes[node][0] if instructions[steps % len(instructions)] == 'L' else nodes[node][1]
|
|
steps += 1
|
|
|
|
|
|
nodeGhost = [n for n in nodes if n[-1] == 'A']
|
|
multiples = []
|
|
|
|
for n in nodeGhost:
|
|
stepsGhost = 0
|
|
nodeTemp = n
|
|
while nodeTemp[-1] != 'Z':
|
|
nodeTemp = nodes[nodeTemp][0] if instructions[stepsGhost % len(instructions)] == 'L' else nodes[nodeTemp][1]
|
|
stepsGhost += 1
|
|
multiples.append(stepsGhost)
|
|
|
|
|
|
print(steps)
|
|
print(lcm(*multiples)) |