88 lines
1.9 KiB
Python
88 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from itertools import chain
|
|
import math
|
|
|
|
dic = {}
|
|
rl = ""
|
|
source = []
|
|
|
|
# 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
|
|
z_count = 0
|
|
ss = s
|
|
out = []
|
|
while z_count < 1:
|
|
if left_or_right(counter) == "R":
|
|
ss = dic[ss][1]
|
|
elif left_or_right(counter) == "L":
|
|
ss = dic[ss][0]
|
|
counter += 1
|
|
out.append(ss)
|
|
if ss.endswith("Z"):
|
|
z_count += 1
|
|
print(s, counter)
|
|
loops.append(counter)
|
|
print(loops)
|
|
|
|
# LCM von math Bibliothek
|
|
print()
|
|
print("Mit math.lcm()")
|
|
print("Kleinstes gemeinsames Vielfaches (Least Common Multiplier):", math.lcm(*loops))
|
|
|
|
|
|
# Eigene LCM-Funktion (Primfaktoren -> multiplizieren)
|
|
print()
|
|
print("Meine eigene lcm..")
|
|
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("erstmal alle Primfaktoren ermitteln")
|
|
print("Teiler-Liste:", teiler_liste)
|
|
|
|
unique_teiler = []
|
|
for teiler in teiler_liste:
|
|
if teiler not in unique_teiler:
|
|
unique_teiler.append(teiler)
|
|
print("ohne doppelte Einträge:", unique_teiler)
|
|
|
|
solution = 1
|
|
for tt in unique_teiler:
|
|
solution *= tt
|
|
print("alles multipliziert -> Lösung:", solution)
|