2022 day 8 part 2 solved

kwout 11 months ago
parent 6fcf272b78
commit acc56c9ab1
  1. 44
      2022/8.py

@ -1,27 +1,49 @@
matrix = [[int(i) for i in j] for j in open("input2.txt", 'r').read().splitlines()]
matrix = [[int(i) for i in j] for j in open("input.txt", 'r').read().splitlines()]
visible = set()
highScore = 0
highScoreCoord = ()
def walk(line):
length = len(line)
vis = [0, length-1]
vis = [0]
tallest = line[0]
tallestInv = line[-1]
for i in range(1, length):
for i in range(1, len(line)):
if line[i] > tallest:
vis.append(i)
tallest = line[i]
if line[length-i] > tallestInv:
vis.append(length-i)
tallestInv = line[-i]
print(vis)
return vis
def findFirst(line, height):
for i in range(len(line)):
if line[i] >= height:
return i+1
return len(line)
for i in range(len(matrix)):
for coord in walk(matrix[i]):
visible.add((i, coord))
for coord in walk(matrix[i][::-1]):
visible.add((i, len(matrix) - (coord) - 1))
for i in range(len(matrix[0])):
for coord in walk([matrix[j][i] for j in range(len(matrix[0]))]):
col = [matrix[j][i] for j in range(len(matrix[0]))]
for coord in walk(col):
visible.add((coord, i))
for coord in walk(col[::-1]):
visible.add((len(matrix[0]) - (coord) - 1, i))
for i in range(len(matrix)):
for j in range(len(matrix[0])):
score = 0
left = findFirst(matrix[i][:j][::-1], matrix[i][j])
right = findFirst(matrix[i][j+1:], matrix[i][j])
col = [matrix[x][j] for x in range(len(matrix[0]))]
up = findFirst(col[:i][::-1], matrix[i][j])
down = findFirst(col[i+1:], matrix[i][j])
score = up*down*left*right
if score > highScore:
highScore = score
highScoreCoord = (i,j)
print(visible)
print(len(visible))
print(highScore)
Loading…
Cancel
Save