81 lines
1.6 KiB
Python
81 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from time import time
|
|
from itertools import chain
|
|
|
|
startzeit = time()
|
|
dic = {}
|
|
rl = ""
|
|
source = []
|
|
counter = 0
|
|
all_end_z = False
|
|
|
|
# parse input
|
|
input_file = open("input", "r")
|
|
for line in input_file:
|
|
line = line.strip()
|
|
if line == "":
|
|
continue
|
|
elif "=" in line:
|
|
line = line.split("=")
|
|
dic[line[0].strip()] = line[1].replace("(", "").replace(")", "").replace(" ", "").split(",")
|
|
else:
|
|
rl = line
|
|
input_file.close()
|
|
|
|
|
|
def left_or_right(zahl):
|
|
j = zahl % len(rl)
|
|
return rl[j]
|
|
|
|
|
|
for key in dic.keys():
|
|
if key.endswith("A"):
|
|
source.append(key)
|
|
|
|
print("Start:", source)
|
|
|
|
loops = []
|
|
for s in source:
|
|
counter = 0
|
|
zcount = 0
|
|
ss = s
|
|
out = []
|
|
while zcount < 1:
|
|
x = left_or_right(counter)
|
|
counter += 1
|
|
if x == "R":
|
|
ss = dic[ss][1]
|
|
elif x == "L":
|
|
ss = dic[ss][0]
|
|
out.append(ss)
|
|
if ss.endswith("Z"):
|
|
zcount += 1
|
|
print(s, counter)
|
|
loops.append(counter)
|
|
print(loops)
|
|
|
|
teiler_liste = []
|
|
for n in loops:
|
|
li = []
|
|
for i in chain([2], range(3, n // 2 + 1, 2)):
|
|
while n % i == 0:
|
|
li.append(i)
|
|
teiler_liste.append(i)
|
|
n = n // i
|
|
if i > n:
|
|
break
|
|
if not li:
|
|
teiler_liste.append(n)
|
|
|
|
print("Teilerliste:", teiler_liste)
|
|
|
|
unique_teiler = []
|
|
for teiler in teiler_liste:
|
|
if teiler not in unique_teiler:
|
|
unique_teiler.append(teiler)
|
|
solution = 1
|
|
for tt in unique_teiler:
|
|
solution *= tt
|
|
print("Lösung:", solution)
|