Add Solve for 13 a
This commit is contained in:
146
13/13.ipynb
Normal file
146
13/13.ipynb
Normal file
@@ -0,0 +1,146 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"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",
|
||||
"\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[:,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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Part 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"28714"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"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 ])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Part 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 225,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[(array([[69., 27.],\n",
|
||||
" [23., 71.]]),\n",
|
||||
" array([10000000018641, 10000000010279]),\n",
|
||||
" array([102851800151, 107526881786]))]"
|
||||
]
|
||||
},
|
||||
"execution_count": 225,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"[x for x in readfile(offset=10000000000000) if x[2] is not None ]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"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.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user