{ "cells": [ { "cell_type": "markdown", "id": "44a4d803-d75a-4954-9395-f496f05cfa3c", "metadata": {}, "source": [ "#### Part 1" ] }, { "cell_type": "code", "execution_count": 82, "id": "472462b7-896e-4993-80c4-996f318c0c44", "metadata": {}, "outputs": [], "source": [ "with open(\"aoc8_input.txt\", \"r\") as f:\n", " forest = []\n", " for line in f:\n", " forest.append([*map(int, line.strip())])\n", "\n", "W, H = len(forest[0]), len(forest)" ] }, { "cell_type": "code", "execution_count": 83, "id": "828ee197-c184-4826-afc4-3700b0e97762", "metadata": {}, "outputs": [], "source": [ "def get_row(row):\n", " return forest[row]\n", "\n", "def get_col(col):\n", " return list(map(lambda row: row[col], forest))\n", "\n", "def is_visible(x: int, y:int) -> bool:\n", " v = forest[x][y]\n", " r, c = get_row(x), get_col(y)\n", " \n", " rlp = r[:y] + [0]\n", " rrp = r[y+1:] + [0]\n", " cup = c[:x] + [0]\n", " cdp = c[x+1:] + [0]\n", " \n", " return max(rlp) >= v and max(rrp) >= v and max(cup) >= v and max(cdp) >= v" ] }, { "cell_type": "code", "execution_count": 84, "id": "92c923a5-b93f-4492-8ac0-afc98ebd0205", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1818" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = 0\n", "total = W * H\n", "\n", "for x in range(1, len(forest)-1):\n", " for y in range(1, len(forest[x])-1):\n", " c += int(is_visible(x, y))\n", "\n", "total - c" ] }, { "cell_type": "markdown", "id": "34161fd4-966e-4536-a338-17835748f372", "metadata": {}, "source": [ "#### Part 2" ] }, { "cell_type": "code", "execution_count": 85, "id": "25132b83-3c45-46e0-80df-89ebb8a431a8", "metadata": {}, "outputs": [], "source": [ "def calculate_scenic_score(x: int, y: int) -> int:\n", " v = forest[x][y]\n", " r, c = get_row(x), get_col(y)\n", " \n", " \n", " rlp = r[:y][::-1]\n", " rrp = r[y+1:]\n", " cup = c[:x][::-1]\n", " cdp = c[x+1:]\n", " \n", " ssl, ssr, ssu, ssd = 0, 0, 0, 0\n", " for t in rlp:\n", " ssl += 1\n", " if t >= v:\n", " break\n", " for t in rrp:\n", " ssr += 1\n", " if t >= v:\n", " break\n", " for t in cup:\n", " ssu += 1\n", " if t >= v:\n", " break\n", " for t in cdp:\n", " ssd += 1\n", " if t >= v:\n", " break\n", " \n", " return ssl * ssr * ssu * ssd" ] }, { "cell_type": "code", "execution_count": 86, "id": "54b7e07c-4e3c-4f3b-bab8-588d11117639", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "368368" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "highest_scenic_score = 0\n", "\n", "for x in range(1, len(forest)-1):\n", " for y in range(1, len(forest[x])-1):\n", " sscore = calculate_scenic_score(x, y)\n", " if sscore > highest_scenic_score:\n", " highest_scenic_score = sscore\n", "\n", "highest_scenic_score" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.7" } }, "nbformat": 4, "nbformat_minor": 5 }