From f96335f8398f4f889ffb4bcc4d920fc76235cdc2 Mon Sep 17 00:00:00 2001 From: Tabledevil Date: Fri, 13 Dec 2024 13:22:14 +0100 Subject: [PATCH] Add 13 Part 2 Solution --- 13/13.ipynb | 81 ++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 54 deletions(-) diff --git a/13/13.ipynb b/13/13.ipynb index ce9733f..7ac84a9 100644 --- a/13/13.ipynb +++ b/13/13.ipynb @@ -2,41 +2,44 @@ "cells": [ { "cell_type": "code", - "execution_count": 7, + "execution_count": 161, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import re\n", "\n", - "def readfile(infilename='testinput',offset=0):\n", - " pattern= re.compile(r'''[XY][+=](\\d+)''') #Pattern to Extract Values\n", - " A=np.zeros((2,2),dtype=int) #Empty 2x2 Matrix\n", - " B=None #Placeholder for PrizePosition\n", + "pattern= re.compile(r'''[XY][+=](\\d+)''') #Pattern to Extract Values\n", "\n", + "def readfile(infilename='testinput',offset=0):\n", " with open(infilename,'r') as infile:\n", " for idx,line in enumerate(infile.readlines()):\n", " line = line.strip()\n", " # on empty line yield result and reset\n", " if line =='':\n", - " if B is not None:\n", - " solution = np.linalg.solve(A.astype(int),B.astype(int))\n", - " # remove non integer Solutions\n", - " solution = solution.astype(int) if np.allclose(solution, np.round(solution)) else None\n", - " yield A,B,solution\n", - " A=np.zeros((2,2))\n", - " B=None\n", " continue\n", " # grep X/Y Values from Line\n", " matches = pattern.findall(line)\n", " if \"Button A\" in line: #A Button Presses are X in my equation > first column of A\n", + " A=np.zeros((2,2),dtype=int)\n", " A[:,0]=matches\n", " if \"Button B\" in line: #B Button Presses are Y in my equation > second column of A\n", " A[:,1]=matches\n", - " if \"Prize:\" in line:\n", - " B=np.array(matches,dtype=int)+[offset,offset] #offset added for part 2" + " if \"Prize:\" in line: #Prize Position is the result of solving the equation > B\n", + " B=np.array(matches,dtype=int)+np.array([offset,offset]) #offset added for part 2\n", + " solution = np.linalg.solve(A.astype(int),B.astype(int),)\n", + " # remove non integer Solutions by rounding and checking validity of solution\n", + " reverse = np.dot(A,np.round(solution))\n", + " validation = np.all(np.equal(reverse,B.astype(int)))\n", + " solution = solution.round().astype(int) if validation else None\n", + " yield solution\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -46,22 +49,22 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "28714" + "np.int64(28753)" ] }, - "execution_count": 8, + "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "sum(a*3 + b for a,b in [x[2] for x in readfile('input') if x[2] is not None ])" + "sum((np.sum(x*[3,1]) for x in readfile('input') if x is not None ))" ] }, { @@ -73,58 +76,28 @@ }, { "cell_type": "code", - "execution_count": 225, + "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[(array([[69., 27.],\n", - " [23., 71.]]),\n", - " array([10000000018641, 10000000010279]),\n", - " array([102851800151, 107526881786]))]" + "np.int64(102718967795500)" ] }, - "execution_count": 225, + "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "[x for x in readfile(offset=10000000000000) if x[2] is not None ]\n" + "sum((np.sum(x*[3,1]) for x in readfile('input',offset=10000000000000) if x is not None ))" ] - }, - { - "cell_type": "code", - "execution_count": 226, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "416082282239" - ] - }, - "execution_count": 226, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sum(a*3 + b for a,b in [x[2] for x in readfile(offset=10000000000000) if x[2] is not None ])\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "advent", "language": "python", "name": "python3" }, @@ -138,7 +111,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.13.0" } }, "nbformat": 4,