Guest User

aoc_2022_day_08

a guest
Dec 9th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. #!/usr/bin/env python3.8
  2. """
  3. 30373
  4. 25512
  5. 65332
  6. 33549
  7. 35390
  8. Each tree is represented as a single digit whose value is its height,
  9. where 0 is the shortest and 9 is the tallest.
  10. A tree is visible if all of the other trees between it and an edge of the grid are shorter than it.
  11. Only consider trees in the same row or column; that is, only look up, down, left, or right from any given tree.
  12. All of the trees around the edge of the grid are visible - since they are already on the edge,
  13. there are no trees to block the view.
  14. In this example, that only leaves the interior nine trees to consider:
  15. The top-left 5 is visible from the left and top.
  16. (It isn't visible from the right or bottom since other trees of height 5 are in the way.)
  17. The top-middle 5 is visible from the top and right.
  18. The top-right 1 is not visible from any direction; for it to be visible,
  19. there would need to only be trees of height 0 between it and an edge.
  20. The left-middle 5 is visible, but only from the right.
  21. The center 3 is not visible from any direction; for it to be visible,
  22. there would need to be only trees of at most height 2 between it and an edge.
  23. The right-middle 3 is visible from the right.
  24. In the bottom row, the middle 5 is visible, but the 3 and 4 are not.
  25. With 16 trees visible on the edge and another 5 visible in the interior,
  26. a total of 21 trees are visible in this arrangement.
  27. """
  28. import sys
  29. import time
  30. from functools import wraps
  31. from pathlib import Path
  32. import re
  33. import pytest
  34.  
  35. INPUTS_DIR = 'inputs'
  36. INPUT_FILE = 'day08.txt'
  37. PROJECT_DIR = Path('.')
  38. FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE
  39. LOOP_NO = 1
  40.  
  41.  
  42. def f_time(f):
  43.     @wraps(f)
  44.     def time_check(*args, **kwargs):
  45.         print(f'{LOOP_NO=}')
  46.         start_time = time.monotonic()
  47.         for i in range(LOOP_NO):
  48.             sizes = f(*args, *kwargs)
  49.         end_time = time.monotonic()
  50.         print(f"It took {end_time-start_time:.10f} seconds to compute")
  51.         return sizes
  52.     return time_check
  53.  
  54.  
  55. def read_data(file_path):
  56.     """Read file"""
  57.     with open(file_path) as f:
  58.         return f.read().strip().splitlines()
  59.  
  60.  
  61. def visible_trees_number(file_path):
  62.     trees = read_data(file_path)
  63.     rows, columns = prepare_rows_columns(trees)
  64.     visible_trees, score = tree_visibility(trees, rows, columns)
  65.     return visible_trees, score
  66.  
  67.  
  68. def tree_visibility(trees, rows, columns):
  69.     v = len(trees)
  70.     h = len(trees[0])
  71.     edge_trees = 2*(h-1) + 2*(v-1)
  72.     visible_trees = 0
  73.     score = 0
  74.     for i in range(1, v - 1):
  75.         for j in range(1, h - 1):
  76.             tree = int(trees[i][j])
  77.             if is_tree_visibile(tree, i, j, rows, columns):
  78.                 visible_trees += 1
  79.             current_score = tree_score(tree, i, j, rows, columns)
  80.             if current_score > score:
  81.                 score = current_score
  82.     return visible_trees + edge_trees, score
  83.  
  84.  
  85. def prepare_rows_columns(trees):
  86.     h = len(trees[0])
  87.     rows = []
  88.     for row in trees:
  89.         rows.append(list(map(int, re.findall(r'\d', row))))
  90.     columns = []
  91.     for i in range(h):
  92.         column = []
  93.         for row in rows:
  94.             column.append(row[i])
  95.         columns.append(column)
  96.     return (rows, columns)
  97.  
  98.  
  99. def is_tree_visibile(tree, pos_v, pos_h, rows, columns):
  100.     good_trees = set(list(range(tree, 10)))
  101.     l_trees = set(rows[pos_v][:pos_h])
  102.     r_trees = set(rows[pos_v][pos_h+1:])
  103.     u_trees = set(columns[pos_h][:pos_v])
  104.     d_trees = set(columns[pos_h][pos_v+1:])
  105.     return not (
  106.         good_trees.intersection(l_trees)
  107.         and good_trees.intersection(r_trees)
  108.         and good_trees.intersection(u_trees)
  109.         and good_trees.intersection(d_trees)
  110.     )
  111.  
  112.  
  113. def tree_score(tree, pos_v, pos_h, rows, columns):
  114.     score = 1
  115.     l_trees = rows[pos_v][:pos_h][::-1]
  116.     r_trees = rows[pos_v][pos_h+1:]
  117.     u_trees = columns[pos_h][:pos_v][::-1]
  118.     d_trees = columns[pos_h][pos_v+1:]
  119.     for trees in [l_trees, r_trees, u_trees, d_trees]:
  120.         for i, _tree in enumerate(trees, 1):
  121.             if _tree >= tree:
  122.                 score *= i
  123.                 break
  124.         else:
  125.             score *= len(trees)
  126.     return score
  127.  
  128.  
  129. ex_output_1 = """
  130. 30373
  131. 25512
  132. 65332
  133. 33549
  134. 35390
  135. """
  136. ex_result_1 = 21
  137. tree_house_max_score = 8
  138.  
  139. ex_output_2 = """
  140. 16465939517022874380
  141. 98702061596432835417
  142. 91507485930236271864
  143. 83291124607345950867
  144. 56433069148802719275
  145. 48402771015326935896
  146. 82450956312719603847
  147. 26357084751340681992
  148. 98705146257291438603
  149. 81293278096057431645
  150. 84279159316208704653
  151. 72974835102466059381
  152. 46154257981968327030
  153. 68852502743970119463
  154. 40653017817322459698
  155. 95231881257446607390
  156. 82908169013764457352
  157. 50108329926748345167
  158. 95302579846610241873
  159. 80607296935112485743
  160. """
  161. ex_result_2 = 174
  162.  
  163.  
  164. @pytest.mark.parametrize('trees_matrix, visible_trees, tree_house_max_score', [
  165.     (ex_output_1, ex_result_1, tree_house_max_score),
  166.     (ex_output_2, ex_result_2, 0)
  167. ]
  168. )
  169. def test_dir_structure(trees_matrix, visible_trees, tree_house_max_score):
  170.     trees = trees_matrix.strip().splitlines()
  171.     rows, columns = prepare_rows_columns(trees)
  172.     current_visible_trees, score = tree_visibility(trees, rows, columns)
  173.     assert (visible_trees == current_visible_trees), (
  174.         f'Incorrect struct\nExpected: {visible_trees}\nActual: {current_visible_trees}'
  175.     )
  176.     if tree_house_max_score:
  177.         assert score == tree_house_max_score
  178.  
  179.  
  180. if __name__ == '__main__':
  181.     file_path = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH
  182.     print(visible_trees_number(file_path))
  183.  
  184.     pytest.main(sys.argv)
  185.  
  186.     import os
  187.     if (loop_no := os.getenv('LOOP_NO')) is None:
  188.         exit()
  189.     LOOP_NO = int(loop_no)
  190.  
Advertisement
Add Comment
Please, Sign In to add comment