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):
print('south')
if x < (len(ruum)-2):
if ruum[x+1][y] in ('|JL'):
return True
return False
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')
print("north")
if x > 0:
if ruum[x-1][y] in ('|7F'):
return True
return False
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')
print("west")
if y > 0:
if ruum[x][y-1] in ('-LF'):
return True
return False
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('south')
if y < (len(ruum[x]) - 2):
if ruum[x][y+1] in ('-J7'):
return True
return False
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 otsing(x, y, tase):
if ruum[x][y] == 'S':
checknorth(x, y)
checksouth(x, y)
checkwest(x, y)
checkeast(x, y)
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] == "|":
checknorth(x, y)
checksouth(x, y)
vastus = checknorth(x, y) + checksouth(x, y)
if ruum[x][y] == "-":
checkeast(x, y)
checkwest(x, y)
vastus = checkeast(x, y) + checkwest(x, y)
if ruum[x][y] == "L":
checknorth(x, y)
checkeast(x, y)
vastus = checknorth(x, y) + checkeast(x, y)
if ruum[x][y] == "J":
checknorth(x, y)
checkwest(x, y)
vastus = checknorth(x, y) + checkwest(x, y)
if ruum[x][y] == "F":
checksouth(x, y)
checkwest(x, y)
vastus = checksouth(x, y) + checkeast(x, y)
if ruum[x][y] == "7":
checksouth(x, y)
checkwest(x, y)
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:
@@ -63,17 +104,21 @@ ruum = []
leitud = []
for line in lines:
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 y, koht in enumerate(rida):
print(f"x={x}, y={y}, koht={koht}")
if koht == 'S':
otsing(x, y, 0)
leitud = True
break
if leitud:
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