2022 day 8
This commit is contained in:
40
2022/08/part1
Executable file
40
2022/08/part1
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
TGRID = [] # heights
|
||||
VGRID = [] # visibility
|
||||
|
||||
def check_visible(x, y, xr, yr):
|
||||
target = TGRID[y][x]
|
||||
for y in yr:
|
||||
for x in xr:
|
||||
if TGRID[y][x] >= target:
|
||||
return False
|
||||
return True
|
||||
|
||||
with open('input') as f:
|
||||
for line in f:
|
||||
l = []
|
||||
for tree in line.rstrip():
|
||||
l.append(int(tree))
|
||||
TGRID.append(l)
|
||||
|
||||
max_y = len(TGRID)
|
||||
for y in range(0, max_y):
|
||||
max_x = len(TGRID[y])
|
||||
l = []
|
||||
for x in range(0, max_x):
|
||||
l.append(
|
||||
check_visible(x, y, range(0, x), range(y, y+1)) or # left to right
|
||||
check_visible(x, y, range(x, x+1), range(0, y)) or # top to bottom
|
||||
check_visible(x, y, range(max_x-1, x, -1), range(y, y+1)) or # right to left
|
||||
check_visible(x, y, range(x, x+1), range(max_y-1, y, -1)) # bottom to top
|
||||
)
|
||||
VGRID.append(l)
|
||||
|
||||
count = 0
|
||||
for row in VGRID:
|
||||
for col in row:
|
||||
if col:
|
||||
count = count + 1
|
||||
|
||||
print(count)
|
Reference in New Issue
Block a user