Advent 10-1

This commit is contained in:
2023-12-27 10:53:28 +02:00
parent cd6617eb67
commit b8bbc9c275

137
10/advent_1.py Normal file → Executable file
View File

@@ -1,59 +1,100 @@
#!/usr/bin/env python3.12
from dataclasses import dataclass
import sys
@dataclass
class Koht:
x: int
y: int
def checksouth(x, y): def checksouth(x, y):
print('south') print("south")
if x < (len(ruum)-2): if x < (len(ruum) - 1):
if ruum[x+1][y] in ('|JL'): print('poleservastyle')
return True if ruum[x + 1][y] in ("|JL") and leitud[x + 1][y] == " ":
return False print("south")
return [Koht(x+1,y)]
return []
def checknorth(x, y): def checknorth(x, y):
print('north') print("north")
if x > 0: if x > 0:
if ruum[x-1][y] in ('|7F'): print('poleservastyle')
return True if ruum[x - 1][y] in ("|7F") and leitud[x - 1][y] == " ":
return False print("north")
return [Koht(x - 1, y)]
return []
def checkwest(x, y): def checkwest(x, y):
print('west') print("west")
if y > 0: if y > 0:
if ruum[x][y-1] in ('-LF'): print('poleservastyle')
return True if ruum[x][y - 1] in ("-LF") and leitud[x][y - 1] == " ":
return False print("west")
return [Koht(x, y - 1)]
return []
def checkeast(x, y): def checkeast(x, y):
print('south') print("east")
if y < (len(ruum[x]) - 2): if y < (len(ruum[x]) - 1):
if ruum[x][y+1] in ('-J7'): print('poleservastyle')
return True if ruum[x][y + 1] in ("-J7") and leitud[x][y + 1] == " ":
return False print("east")
return [Koht(x, y + 1)]
return []
def otsing(x, y, tase): def ruumikorvalon(x, y):
if ruum[x][y] == 'S': vastus = []
checknorth(x, y) if ruum[x][y] == "S":
checksouth(x, y) vastus = checknorth(x, y) + checksouth(x, y) + checkwest(x, y) + checkeast(x, y)
checkwest(x, y)
checkeast(x, y)
if ruum[x][y] == "|": if ruum[x][y] == "|":
checknorth(x, y) vastus = checknorth(x, y) + checksouth(x, y)
checksouth(x, y)
if ruum[x][y] == "-": if ruum[x][y] == "-":
checkeast(x, y) vastus = checkeast(x, y) + checkwest(x, y)
checkwest(x, y)
if ruum[x][y] == "L": if ruum[x][y] == "L":
checknorth(x, y) vastus = checknorth(x, y) + checkeast(x, y)
checkeast(x, y)
if ruum[x][y] == "J": if ruum[x][y] == "J":
checknorth(x, y) vastus = checknorth(x, y) + checkwest(x, y)
checkwest(x, y)
if ruum[x][y] == "F": if ruum[x][y] == "F":
checksouth(x, y) vastus = checksouth(x, y) + checkeast(x, y)
checkwest(x, y)
if ruum[x][y] == "7": if ruum[x][y] == "7":
checksouth(x, y) vastus = checksouth(x, y) + checkwest(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: with open("data.txt") as file:
@@ -63,17 +104,21 @@ ruum = []
leitud = [] leitud = []
for line in lines: for line in lines:
ruum.append([*line]) ruum.append([*line])
leitud.append([*line]) leitud.append([" "] * len(line))
print(ruum)
leitud = False # kuva(leitud)
# kuva(ruum)
leitudS = False
for x, rida in enumerate(ruum): for x, rida in enumerate(ruum):
for y, koht in enumerate(rida): print(list(enumerate(rida)))
print(f"x={x}, y={y}, koht={koht}") for y, asukoht in enumerate(rida):
if koht == 'S': print(f"x={x}, y={y}, koht={asukoht}")
otsing(x, y, 0) if asukoht == "S":
leitud = True otsing(x, y)
break leitudS = True
if leitud: break
if leitudS:
break break