48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
from math import gcd
|
|
import sys
|
|
|
|
steps, _, *rest = open(sys.argv[1]).read().splitlines()
|
|
|
|
network = {}
|
|
|
|
for line in rest:
|
|
pos, targets = line.split(" = ")
|
|
network[pos] = targets[1:-1].split(", ")
|
|
|
|
positions = [key for key in network if key.endswith("A")]
|
|
cycles = []
|
|
|
|
for current in positions:
|
|
cycle = []
|
|
|
|
current_steps = steps
|
|
step_count = 0
|
|
first_z = None
|
|
|
|
while True:
|
|
print(current_steps)
|
|
while step_count == 0 or not current.endswith("Z"):
|
|
step_count += 1
|
|
current = network[current][0 if current_steps[0] == "L" else 1]
|
|
print(current)
|
|
current_steps = current_steps[1:] + current_steps[0]
|
|
print(current_steps)
|
|
cycle.append(step_count)
|
|
|
|
if first_z is None:
|
|
first_z = current
|
|
step_count = 0
|
|
elif current == first_z:
|
|
break
|
|
|
|
cycles.append(cycle)
|
|
|
|
nums = [cycle[0] for cycle in cycles]
|
|
|
|
lcm = nums.pop()
|
|
|
|
for num in nums:
|
|
lcm = lcm * num // gcd(lcm, num)
|
|
|
|
print(lcm)
|