Add Solution for 15

This commit is contained in:
tobias
2024-12-16 23:07:16 +01:00
parent 6449d09b11
commit af2ea4b6dc
4 changed files with 26932 additions and 0 deletions

161
15/15.ipynb Normal file
View File

@@ -0,0 +1,161 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
"raw_warehouse = []\n",
"raw_route = []\n",
"reading_map = True\n",
"with open('testinput','r') as infile:\n",
" for line in data:\n",
" line = line.strip()\n",
" if reading_map:\n",
" if len(line) >0:\n",
" raw_warehouse.append(list(line))\n",
" else:\n",
" reading_map = False\n",
" else:\n",
" for char in line:\n",
" if char in \"<>v^\":\n",
" raw_route.append(char)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"warehouse=np.array(raw_warehouse)"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"conv_direction = {\n",
" '<' : np.array([0,-1]),\n",
" '>' : np.array([0,1]),\n",
" '^' : np.array([-1,0]),\n",
" 'v' : np.array([1,0])\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
"def in_bounds(pos,warehouse):\n",
" return pos[0] >= 0 and pos[0]<len(warehouse) and pos[1] >= 0 and pos[1] < len(warehouse[pos[0]])"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"def next_pos(pos,direction,warehouse):\n",
" return tuple(np.array(pos) + conv_direction[direction])\n",
" # return newpos if in_bounds(newpos,warehouse) else None\n"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"def can_move(pos,direction,warehouse):\n",
" new_pos = next_pos(pos,direction,warehouse)\n",
" if in_bounds(new_pos,warehouse):\n",
" match warehouse[new_pos]:\n",
" case '.':\n",
" return True\n",
" case '#':\n",
" return False\n",
" case 'O':\n",
" # Find next wall in that direction and look for at least on free space\n",
" cursor = new_pos\n",
" \n",
" \n",
"\n",
" return False\n"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"def get_locations(warehouse,target='@'):\n",
" return list(zip(*np.where(warehouse==target)))"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"can_move(get_locations(warehouse)[0],'<',warehouse)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pdf",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}