132 lines
3.3 KiB
Python
132 lines
3.3 KiB
Python
#!/usr/bin/env python3.12
|
|
|
|
import re
|
|
|
|
|
|
class RL:
|
|
def __init__(self, L, R):
|
|
self.directions = {}
|
|
self.directions["R"] = R
|
|
self.directions["L"] = L
|
|
|
|
def get(self, direction):
|
|
return self.directions[direction]
|
|
|
|
def __str__(self):
|
|
return f"L: {self.get('L')} R: {self.get('R')}"
|
|
|
|
|
|
with open("data2.txt") as file:
|
|
lines = [line.rstrip() for line in file]
|
|
|
|
|
|
x = 0
|
|
maps = {}
|
|
algused = []
|
|
for line in lines:
|
|
print(line)
|
|
if x == 0:
|
|
instructions = line
|
|
x += 1
|
|
continue
|
|
if line == "":
|
|
print("Tühi rida")
|
|
continue
|
|
temp = re.findall(r"\w+", line)
|
|
z = list(temp)
|
|
maps[z[0]] = RL(z[1], z[2])
|
|
if z[0][2] == "A":
|
|
print(f"Lisan alguse {z[0]}")
|
|
algused.append(z[0])
|
|
|
|
print(instructions)
|
|
print(algused)
|
|
|
|
|
|
teemeveel = True
|
|
x = 0
|
|
# while teemeveel:
|
|
# # print(f"Samm {instructions[x % len(instructions)]} {instructions} {x}")
|
|
# # if x % (len(instructions)*100) == 0:
|
|
# # print(x)
|
|
# for idx, vastus in enumerate(algused):
|
|
# # print(algused[idx])
|
|
# algused[idx] = maps[vastus].get(instructions[x % len(instructions)])
|
|
# # print(algused[idx])
|
|
|
|
# # print(f"Samm {x}")
|
|
# for i in algused:
|
|
# teemeveel = False
|
|
# # print(f"I on {i}")
|
|
# if i[2] != 'Z':
|
|
# # print('Edasi on vaja teha')
|
|
# teemeveel = True
|
|
# break
|
|
# # print(teemeveel)
|
|
|
|
# x += 1
|
|
|
|
|
|
|
|
print(f"Juhiste pikkus on {len(instructions)}")
|
|
for vastus in algused:
|
|
alustasin = vastus
|
|
sammud = {}
|
|
kohad = {}
|
|
# for i in range(0,len(instructions)):
|
|
# sammud[i] = []
|
|
# print(f"Samm {instructions[x % len(instructions)]} {instructions} {x}")
|
|
# if x % (len(instructions)*100) == 0:
|
|
# print(x)
|
|
#print(vastus)
|
|
#while vastus[2] != "Z":
|
|
# while vastus not in sammud[x % len(instructions)]:
|
|
# sammud[i].append(vastus)
|
|
# vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
# x += 1
|
|
# jargmine = maps[vastus].get(instructions[x % len(instructions)])
|
|
# print(f"{vastus} -> {jargmine}")
|
|
# print(x)
|
|
while vastus[2] != "Z":
|
|
#sammud[i].append(vastus)
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
x += 1
|
|
print(f"alustasin {alustasin} {vastus}")
|
|
k1 = x
|
|
print(x)
|
|
x +=1
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
while vastus[2] != "Z":
|
|
#sammud[i].append(vastus)
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
x += 1
|
|
print(f"alustasin {alustasin} {vastus}")
|
|
k2 = x
|
|
print(x)
|
|
x +=1
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
while vastus[2] != "Z":
|
|
#sammud[i].append(vastus)
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
x += 1
|
|
|
|
k3 = x
|
|
x +=1
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
|
|
while vastus[2] != "Z":
|
|
#sammud[i].append(vastus)
|
|
vastus = maps[vastus].get(instructions[x % len(instructions)])
|
|
x += 1
|
|
|
|
|
|
print(f"alustasin {alustasin} {vastus}")
|
|
print(x)
|
|
k4 = x
|
|
|
|
print(f"{k2-k1} {k3-k2} {k4-k3}")
|
|
print("######")
|
|
# print(f"Samm {instructions[x % len(instructions)]} {instructions} {x}")
|
|
# if x % (len(instructions)*100) == 0:
|
|
# print(x)
|