{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# AoC_2018_D3 " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "myfabric = [[0 for n in range(1000)] for p in range(1000)]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfabric[3][5]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "with open('day3.txt') as f:\n", " mydata = f.read().splitlines()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1399" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(mydata)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'#1 @ 669,271: 17x11'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydata[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "must be str or None, not list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmydata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'@'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m','\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m':'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: must be str or None, not list" ] } ], "source": [ "mydata[0].split(['@',',',':','x'])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['#1 ', '669', '271', '17', '11']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.split('@ |,|: |x', mydata[0])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "nums = [int(n) for n in re.split('@ |,|: |x', mydata[0])[1:]]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[669, 271, 17, 11]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "nums = [[int(n) for n in re.split('@ |,|: |x', elf)[1:]] for elf in mydata]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[669, 271, 17, 11],\n", " [153, 186, 20, 26],\n", " [186, 838, 28, 11],\n", " [119, 248, 18, 13]]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums[:4]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[727, 567, 17, 25],\n", " [52, 176, 19, 21],\n", " [453, 308, 19, 24],\n", " [238, 370, 21, 18],\n", " [724, 519, 23, 10]]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums[-5:]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# fabric array \n", "a = np.zeros(shape=(1000,1000))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(669, 271, 17, 11)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x,y,xd,yd = nums[0]\n", "x,y,xd,yd" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "for e in nums:\n", " x,y,xd,yd = e\n", " a[y:y+yd,x:x+xd] += 1\n" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "118223" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(a > 1).sum()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "index 1000 is out of bounds for axis 0 with size 1000", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1000\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1000\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: index 1000 is out of bounds for axis 0 with size 1000" ] } ], "source": [ "a[0,0],a[3,3]," ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "b = np.zeros(shape=(10,10))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "b[3:7,1:5] += 1" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 1., 1., 1., 1., 0., 0., 0.],\n", " [0., 0., 0., 1., 1., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 2., 2., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 2., 2., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[1:5,3:7] += 1\n", "b" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 1., 1., 1., 1., 0., 0., 0.],\n", " [0., 0., 0., 1., 1., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 2., 2., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 2., 2., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 1., 1., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 1., 1., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[5:7,5:7] += 1\n", "b" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(b > 1).sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "--- Part Two ---\n", "Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!\n", "\n", "For example, in the claims above, only claim 3 is intact after all claims are made.\n", "\n", "What is the ID of the only claim that doesn't overlap?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }