illuminati229

Untitled

Dec 8th, 2022
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4. def day08(filepath, scenic_score=False):
  5.     trees = np.genfromtxt(filepath, delimiter=1, dtype=int)
  6.     if not scenic_score:
  7.         visible_trees = sum(trees.shape) * 2 - 4
  8.         for index, value in np.ndenumerate(trees):
  9.             if index[0] == 0 or index[1] == 0 or index[0] == trees.shape[0]-1 or index[1] == trees.shape[1]-1:
  10.                 continue
  11.             # up
  12.             if trees[:index[0], index[1]].max() < value:
  13.                 visible_trees += 1
  14.             # down
  15.             elif trees[index[0]+1:, index[1]].max() < value:
  16.                 visible_trees += 1
  17.             # left
  18.             elif trees[index[0], :index[1]].max() < value:
  19.                 visible_trees += 1
  20.             # right
  21.             elif trees[index[0], index[1]+1:].max() < value:
  22.                 visible_trees += 1
  23.  
  24.         return visible_trees
  25.     else:
  26.         scenic_score = []
  27.         for index, value in np.ndenumerate(trees):
  28.             if index[0] == 0 or index[1] == 0 or index[0] == trees.shape[0]-1 or index[1] == trees.shape[1]-1:
  29.                 continue
  30.             # up
  31.             scenic_score.append(1)
  32.             up_score = 0
  33.             for sub_value in np.flip(trees[:index[0], index[1]]):
  34.                 if sub_value < value:
  35.                     up_score += 1
  36.                 else:
  37.                     up_score += 1
  38.                     break
  39.             # down
  40.             down_score = 0
  41.             for sub_value in trees[index[0]+1:, index[1]]:
  42.                 if sub_value < value:
  43.                     down_score += 1
  44.                 else:
  45.                     down_score += 1
  46.                     break
  47.             # left
  48.             left_score = 0
  49.             for sub_value in np.flip(trees[index[0], :index[1]]):
  50.                 if sub_value < value:
  51.                     left_score += 1
  52.                 else:
  53.                     left_score += 1
  54.                     break
  55.             # right
  56.             right_score = 0
  57.             for sub_value in trees[index[0], index[1]+1:]:
  58.                 if sub_value < value:
  59.                     right_score += 1
  60.                 else:
  61.                     right_score += 1
  62.                     break
  63.  
  64.             scenic_score[-1] = up_score * down_score * left_score * right_score
  65.  
  66.         return max(scenic_score)
  67.  
  68.  
  69. def main():
  70.     assert day08('test08') == 21
  71.     print(day08('input08'))
  72.  
  73.     assert day08('test08', True) == 8
  74.     print(day08('input08', True))
  75.  
  76.  
  77. if __name__ == '__main__':
  78.     main()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment