#!/usr/bin/env python3.12 from dataclasses import dataclass import sys @dataclass class Koht: x: int y: int def checksouth(x, y): print("south") if x < (len(ruum) - 1): print('poleservastyle') if ruum[x + 1][y] in ("|JL") and leitud[x + 1][y] == " ": print("south") return [Koht(x+1,y)] return [] def checknorth(x, y): print("north") if x > 0: print('poleservastyle') if ruum[x - 1][y] in ("|7F") and leitud[x - 1][y] == " ": print("north") return [Koht(x - 1, y)] return [] def checkwest(x, y): print("west") if y > 0: print('poleservastyle') if ruum[x][y - 1] in ("-LF") and leitud[x][y - 1] == " ": print("west") return [Koht(x, y - 1)] return [] def checkeast(x, y): print("east") if y < (len(ruum[x]) - 1): print('poleservastyle') if ruum[x][y + 1] in ("-J7") and leitud[x][y + 1] == " ": print("east") return [Koht(x, y + 1)] return [] def ruumikorvalon(x, y): vastus = [] if ruum[x][y] == "S": vastus = checknorth(x, y) + checksouth(x, y) + checkwest(x, y) + checkeast(x, y) if ruum[x][y] == "|": vastus = checknorth(x, y) + checksouth(x, y) if ruum[x][y] == "-": vastus = checkeast(x, y) + checkwest(x, y) if ruum[x][y] == "L": vastus = checknorth(x, y) + checkeast(x, y) if ruum[x][y] == "J": vastus = checknorth(x, y) + checkwest(x, y) if ruum[x][y] == "F": vastus = checksouth(x, y) + checkeast(x, y) if ruum[x][y] == "7": vastus = checksouth(x, y) + checkwest(x, y) return vastus def otsing(x, y): mitmes = 0 kasonveel = True vajateha = [Koht(x, y)] while kasonveel: kasonveel = False jargmised = [] for i in vajateha: print(i) leitud[i.x][i.y] = str(mitmes) print(f"mitmes on praegu {mitmes}") jargmised += ruumikorvalon(i.x, i.y) if len(jargmised)> 0: kasonveel = True else: print(f"Vastus on {mitmes}") vajateha = jargmised.copy() #kuva(leitud) mitmes += 1 def kuva(n): for i in n: print("#" + "".join(i) + "#") print() with open("data.txt") as file: lines = [line.rstrip() for line in file] ruum = [] leitud = [] for line in lines: ruum.append([*line]) leitud.append([" "] * len(line)) # kuva(leitud) # kuva(ruum) leitudS = False for x, rida in enumerate(ruum): print(list(enumerate(rida))) for y, asukoht in enumerate(rida): print(f"x={x}, y={y}, koht={asukoht}") if asukoht == "S": otsing(x, y) leitudS = True break if leitudS: break