finished day 8

This commit is contained in:
Orangerot 2022-12-17 01:58:22 +01:00
parent 6596b48113
commit 4a894a7b95
2 changed files with 60 additions and 14 deletions

View file

@ -2,35 +2,41 @@ import sys
def isVisible(lines, row, col): def isVisible(lines, row, col):
scenic = 1 scenic = 1
# up
score1 = 0 score1 = 0
for x in lines[:row:-1]: for x in lines[:row][::-1]:
score1 += 1
if ( x[col] >= lines[row][col]): if ( x[col] >= lines[row][col]):
break break
score1 += 1
scenic *= score1 scenic *= score1
score2 = 0 # left
for x in lines[row+1:]:
if ( x[col] >= lines[row][col]):
break
score2 += 1
scenic *= score2
score3 = 0 score3 = 0
for x in lines[row][:col:-1]: for x in lines[row][:col-1][::-1]:
score3 += 1
if ( x >= lines[row][col]): if ( x >= lines[row][col]):
break break
score3 += 1
scenic *= score3 scenic *= score3
# right
score4 = 0 score4 = 0
for x in lines[row][col:]: for x in lines[row][col+1:]:
score4 += 1
if ( x >= lines[row][col]): if ( x >= lines[row][col]):
break break
score4 += 1
scenic *= score4 scenic *= score4
print(row, col, score1, score2, score3, score4, scenic) # down
score2 = 0
for x in lines[row:]:
score2 += 1
if ( x[col] >= lines[row][col]):
break
scenic *= score2
print(row, col, "|", score1, score2, score3, score4, "|", scenic)
return scenic return scenic
with open(sys.argv[1], "r") as file: with open(sys.argv[1], "r") as file:

40
2022/day08/main3.py Normal file
View file

@ -0,0 +1,40 @@
import sys
def isVisible(lines, row, col):
scenic = 1
score = [1,2,3,4]
score[0] = [x[col] < lines[row][col] for x in lines[:row][::-1]]
score[1] = [x[col] < lines[row][col] for x in lines[row+1:]]
score[2] = [x < lines[row][col] for x in lines[row][:col][::-1]]
score[3] = [x < lines[row][col] for x in lines[row][col+1:]]
for a in score:
# print(a)
dist = 0
if (len(a) == 0):
dist = 0
elif (all(a)):
dist = len(a)
else:
dist = a.index(False) + 1
# print(dist)
scenic *= dist
print(row, col, "|", scenic)
return scenic
with open(sys.argv[1], "r") as file:
lines = file.readlines()
for a in range(len(lines)):
lines[a] = lines[a].rstrip()
# a = a.rstrip()
best=0
for row in range(len(lines)):
for col in range(len(lines[0])):
# print(lines[row][col], end="")
yes = isVisible(lines, row, col)
best = max(best, yes)
print(best)