withopen('08.txt', 'r') as fp: lines = fp.readlines() grid = [[int(c) for c in line.strip()] for line in lines] grid = np.array(grid) n_r, n_c = grid.shape
Part One
1 2 3 4 5 6 7 8 9 10 11
visible = 0 for i inrange(n_r): for j inrange(n_c): h = grid[i][j] w = grid[i,0:j] e = grid[i,j+1:] n = grid[0:i,j] s = grid[i+1:,j] ifany([all(h > _ for _ indir) fordirin [w, e, n, s]]): visible += 1 print(visible)
defview_dist(h, view): dist = 0 for v in view: dist += 1 if v >= h: break return dist
score = 0 for i inrange(n_r): for j inrange(n_c): h = grid[i][j] w = grid[i,0:j][::-1] e = grid[i,j+1:] n = grid[0:i,j][::-1] s = grid[i+1:,j] score = max(score, (view_dist(h, w) * view_dist(h, e) * view_dist(h, n) * view_dist(h, s))) print(score)