# AOC day 8

Dec 8th, 2022 (edited)
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/env python3
2.
3. with open("input.txt") as file:
4.     data=[[tree for tree in grid.strip()] for grid in file]
5.
6. def visible(x,y,grid):
7.     height=grid[x][y]
8.
9.     visibles=0
10.
11.     for i in range(0,x):
12.         if not i==x and grid[i][y]>=height:
13.             visibles-=1
14.             break
15.     visibles+=1
16.
17.     for i in range(x,len(grid)):
18.         if not i==x and grid[i][y]>=height:
19.             visibles-=1
20.             break
21.     visibles+=1
22.
23.
24.     for i in range(0,y):
25.         if not i==y and grid[x][i]>=height:
26.             visibles-=1
27.             break
28.     visibles+=1
29.
30.     for i in range(y,len(grid[x])):
31.         if not i==y and grid[x][i]>=height:
32.             visibles-=1
33.             break
34.     visibles+=1
35.
36.     return True if visibles else False
37.
38. margin=(2*len(data[0])+2*(len(data)-2))
39. print("Part 1:", sum([sum([1 for col in range(1,len(data[row])-1) if visible(row,col,data)]) for row in range(1,len(data)-1)])+margin)
40.
41. ########################################################################################################
42.
43. #!/usr/bin/env python3
44.
45. with open("input.txt") as file:
46.     data=[[tree for tree in grid.strip()] for grid in file]
47.
48. def score(x,y,grid):
49.     height=grid[x][y]
50.
51.     tree_score=[0,0,0,0]
52.
53.     for i in range(x-1,-1,-1):
54.         if grid[i][y]<height:
55.             tree_score[0]+=1
56.         if grid[i][y]>=height:
57.             tree_score[0]+=1
58.             break
59.
60.     for i in range(x+1,len(grid)):
61.         if grid[i][y]<height:
62.             tree_score[1]+=1
63.         if grid[i][y]>=height:
64.             tree_score[1]+=1
65.             break
66.
67.     for i in range(y-1,-1,-1):
68.         if grid[x][i]<height:
69.             tree_score[2]+=1
70.         if grid[x][i]>=height:
71.             tree_score[2]+=1
72.             break
73.
74.     for i in range(y+1,len(grid[x])):
75.         if grid[x][i]<height:
76.             tree_score[3]+=1
77.         if grid[x][i]>=height:
78.             tree_score[3]+=1
79.             break
80.
81.     return tree_score[0]*tree_score[1]*tree_score[2]*tree_score[3]
82.
83. print("Part 2:", sorted([max([score(row,col,data) for col in range(len(data[0]))]) for row in range(len(data))])[-1])
84.