From b09363fb4b8acdfca07dd97b029f3b7c4cbc3c47 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Sun, 1 Dec 2024 11:36:06 +0100 Subject: [PATCH] chore: half-baked 2022 day 8 --- 2022/8/main.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2022/8/main.py diff --git a/2022/8/main.py b/2022/8/main.py new file mode 100644 index 0000000..f128ab0 --- /dev/null +++ b/2022/8/main.py @@ -0,0 +1,56 @@ +input = open('input.txt', 'r').read().strip().splitlines() + +def count_visible_trees(grid): + rows = len(grid) + cols = len(grid[0]) + visible_trees = 0 + + for row in range(rows): + for col in range(cols): + if is_visible(grid, row, col): + visible_trees += 1 + + return visible_trees + +def is_visible(grid, row, col): + height = grid[row][col] + rows = len(grid) + cols = len(grid[0]) + + # Trees on the edge are always visible + if row == 0 or col == 0 or row == rows - 1 or col == cols - 1: + return True + + # Check visibility from left + if all(grid[row][c] < height for c in range(col)): + return True + + # Check visibility from right + if all(grid[row][c] < height for c in range(col + 1, cols)): + return True + + # Check visibility from top + if all(grid[r][col] < height for r in range(row)): + return True + + # Check visibility from bottom + if all(grid[r][col] < height for r in range(row + 1, rows)): + return True + + return False + +# split hte input like the grid variable +grid = [list(map(int, row.split())) for row in input] +print(grid) + +result = count_visible_trees(grid) +print(f"Number of visible trees: {result}") + +def part1(): + return + +def part2(): + return + +print('Part1:', part1()) +print('Part2:', part2()) \ No newline at end of file