#!/usr/bin/env python3 # -*- coding: utf-8 -*- from time import time from itertools import chain startzeit = time() 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) 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("Teiler-Liste:", 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)