{ "cells": [ { "cell_type": "code", "execution_count": 69, "id": "configured-breath", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def new_grid(h, w):\n", " return np.zeros((h, w), dtype=np.int64)\n", " \n", "def turn_on(g, f, t, min=0, max=1):\n", " g[f[0]:t[0]+1, f[1]:t[1]+1] += 1\n", " np.clip(g, min, max, out=g)\n", " \n", "def turn_off(g, f, t, min=0, max=1):\n", " g[f[0]:t[0]+1, f[1]:t[1]+1] -= 1\n", " np.clip(g, min, max, out=g)\n", " \n", "def toggle(g, f, t, min=0, max=1):\n", " if min == 0 and max == 1:\n", " g[f[0]:t[0]+1, f[1]:t[1]+1] = (g[f[0]:t[0]+1, f[1]:t[1]+1] + 1) % 2\n", " else:\n", " g[f[0]:t[0]+1, f[1]:t[1]+1] += 2\n", " \n", " np.clip(g, min, max, out=g)\n", " \n", "def action(s):\n", " a = None\n", " if s.startswith('turn on'):\n", " s = s[len('turn on '):]\n", " a = 1\n", " elif s.startswith('turn off'):\n", " s = s[len('turn off '):]\n", " a = 2\n", " elif s.startswith('toggle'):\n", " s = s[len('toggle '):]\n", " a = 3\n", " else:\n", " return None\n", " \n", " fields = s.split(' ')\n", " f = fields[0].split(',')\n", " t = fields[2].split(',')\n", " \n", " f[0] = int(f[0])\n", " f[1] = int(f[1])\n", " t[0] = int(t[0])\n", " t[1] = int(t[1])\n", "\n", " return (f, t, a)\n", "\n", "def apply_action(g, a, min=0, max=1):\n", " if a[2] == 1:\n", " turn_on(g, a[0], a[1], min, max)\n", " elif a[2] == 2:\n", " turn_off(g, a[0], a[1], min, max)\n", " elif a[2] == 3:\n", " toggle(g, a[0], a[1], min, max)\n", " else:\n", " return" ] }, { "cell_type": "code", "execution_count": 70, "id": "amber-yacht", "metadata": {}, "outputs": [], "source": [ "g = new_grid(1000, 1000)\n", "turn_on(g, (0,0), (999,999))\n", "assert np.sum(g) == 1000*1000\n", "toggle(g, (0,0), (999,0))\n", "assert np.sum(g) == 1000*1000 - 1000\n", "turn_off(g, (499,499), (500,500))\n", "assert np.sum(g) == 1000*1000 - 1000 - 4" ] }, { "cell_type": "code", "execution_count": 71, "id": "favorite-pontiac", "metadata": {}, "outputs": [], "source": [ "import import_ipynb\n", "import helper\n", "s = helper.read_file('2015_6.txt')" ] }, { "cell_type": "code", "execution_count": 72, "id": "fossil-modern", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "569999" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = new_grid(1000, 1000)\n", "for l in s.splitlines():\n", " apply_action(g, action(l))\n", "np.sum(g)" ] }, { "cell_type": "code", "execution_count": 75, "id": "attempted-cemetery", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17836115" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = new_grid(1000, 1000)\n", "for l in s.splitlines():\n", " apply_action(g, action(l), min=0, max=None)\n", "np.sum(g)" ] }, { "cell_type": "code", "execution_count": null, "id": "smooth-locking", "metadata": {}, "outputs": [], "source": [] } ], "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.11.2" }, "title": "Probably a Fire Hazard" }, "nbformat": 4, "nbformat_minor": 5 }