59 lines
1.1 KiB
Python
59 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from time import time
|
|
|
|
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)
|
|
|
|
|
|
def all_z_at_end():
|
|
for s in source:
|
|
if s[2] != "Z":
|
|
return False
|
|
return True
|
|
|
|
|
|
while not all_z_at_end():
|
|
x = left_or_right(counter)
|
|
counter += 1
|
|
for i in range(len(source)):
|
|
if x == "R":
|
|
source[i] = dic[source[i]][1]
|
|
elif x == "L":
|
|
source[i] = dic[source[i]][0]
|
|
|
|
print("End:", source)
|
|
print("RL-Folge", rl)
|
|
print("dictonary:", dic)
|
|
print(f"Lösungschritte: {counter}")
|
|
print(f"benötigte Zeit in s: {int(time()-startzeit)}")
|