#!/usr/bin/env python3.8 """ 30373 25512 65332 33549 35390 Each tree is represented as a single digit whose value is its height, where 0 is the shortest and 9 is the tallest. A tree is visible if all of the other trees between it and an edge of the grid are shorter than it. Only consider trees in the same row or column; that is, only look up, down, left, or right from any given tree. All of the trees around the edge of the grid are visible - since they are already on the edge, there are no trees to block the view. In this example, that only leaves the interior nine trees to consider: The top-left 5 is visible from the left and top. (It isn't visible from the right or bottom since other trees of height 5 are in the way.) The top-middle 5 is visible from the top and right. The top-right 1 is not visible from any direction; for it to be visible, there would need to only be trees of height 0 between it and an edge. The left-middle 5 is visible, but only from the right. The center 3 is not visible from any direction; for it to be visible, there would need to be only trees of at most height 2 between it and an edge. The right-middle 3 is visible from the right. In the bottom row, the middle 5 is visible, but the 3 and 4 are not. With 16 trees visible on the edge and another 5 visible in the interior, a total of 21 trees are visible in this arrangement. """ import sys import time from functools import wraps from pathlib import Path import re import pytest INPUTS_DIR = 'inputs' INPUT_FILE = 'day08.txt' PROJECT_DIR = Path('.') FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE LOOP_NO = 1 def f_time(f): @wraps(f) def time_check(*args, **kwargs): print(f'{LOOP_NO=}') start_time = time.monotonic() for i in range(LOOP_NO): sizes = f(*args, *kwargs) end_time = time.monotonic() print(f"It took {end_time-start_time:.10f} seconds to compute") return sizes return time_check def read_data(file_path): """Read file""" with open(file_path) as f: return f.read().strip().splitlines() def visible_trees_number(file_path): trees = read_data(file_path) rows, columns = prepare_rows_columns(trees) visible_trees, score = tree_visibility(trees, rows, columns) return visible_trees, score def tree_visibility(trees, rows, columns): v = len(trees) h = len(trees[0]) edge_trees = 2*(h-1) + 2*(v-1) visible_trees = 0 score = 0 for i in range(1, v - 1): for j in range(1, h - 1): tree = int(trees[i][j]) if is_tree_visibile(tree, i, j, rows, columns): visible_trees += 1 current_score = tree_score(tree, i, j, rows, columns) if current_score > score: score = current_score return visible_trees + edge_trees, score def prepare_rows_columns(trees): h = len(trees[0]) rows = [] for row in trees: rows.append(list(map(int, re.findall(r'\d', row)))) columns = [] for i in range(h): column = [] for row in rows: column.append(row[i]) columns.append(column) return (rows, columns) def is_tree_visibile(tree, pos_v, pos_h, rows, columns): good_trees = set(list(range(tree, 10))) l_trees = set(rows[pos_v][:pos_h]) r_trees = set(rows[pos_v][pos_h+1:]) u_trees = set(columns[pos_h][:pos_v]) d_trees = set(columns[pos_h][pos_v+1:]) return not ( good_trees.intersection(l_trees) and good_trees.intersection(r_trees) and good_trees.intersection(u_trees) and good_trees.intersection(d_trees) ) def tree_score(tree, pos_v, pos_h, rows, columns): score = 1 l_trees = rows[pos_v][:pos_h][::-1] r_trees = rows[pos_v][pos_h+1:] u_trees = columns[pos_h][:pos_v][::-1] d_trees = columns[pos_h][pos_v+1:] for trees in [l_trees, r_trees, u_trees, d_trees]: for i, _tree in enumerate(trees, 1): if _tree >= tree: score *= i break else: score *= len(trees) return score ex_output_1 = """ 30373 25512 65332 33549 35390 """ ex_result_1 = 21 tree_house_max_score = 8 ex_output_2 = """ 16465939517022874380 98702061596432835417 91507485930236271864 83291124607345950867 56433069148802719275 48402771015326935896 82450956312719603847 26357084751340681992 98705146257291438603 81293278096057431645 84279159316208704653 72974835102466059381 46154257981968327030 68852502743970119463 40653017817322459698 95231881257446607390 82908169013764457352 50108329926748345167 95302579846610241873 80607296935112485743 """ ex_result_2 = 174 @pytest.mark.parametrize('trees_matrix, visible_trees, tree_house_max_score', [ (ex_output_1, ex_result_1, tree_house_max_score), (ex_output_2, ex_result_2, 0) ] ) def test_dir_structure(trees_matrix, visible_trees, tree_house_max_score): trees = trees_matrix.strip().splitlines() rows, columns = prepare_rows_columns(trees) current_visible_trees, score = tree_visibility(trees, rows, columns) assert (visible_trees == current_visible_trees), ( f'Incorrect struct\nExpected: {visible_trees}\nActual: {current_visible_trees}' ) if tree_house_max_score: assert score == tree_house_max_score if __name__ == '__main__': file_path = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH print(visible_trees_number(file_path)) pytest.main(sys.argv) import os if (loop_no := os.getenv('LOOP_NO')) is None: exit() LOOP_NO = int(loop_no)