144 lines
3.5 KiB
Plaintext
144 lines
3.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"with open('input','r') as f:\n",
|
|
" data = f.read()\n",
|
|
"data=data.splitlines()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def find_zeros(data):\n",
|
|
" zeros = []\n",
|
|
" for x,line in enumerate(data):\n",
|
|
" for y,char in enumerate(line):\n",
|
|
" if char == '0':\n",
|
|
" zeros.append((x,y))\n",
|
|
"\n",
|
|
" return zeros\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Recursive Function to to check each direction and recursively call itself until it reaches the end of the path. If it reaches the end of the path, it returns the current position as a solution. If it doesn't reach the end of the path, it continues to check the next direction. The function takes in the current x and y coordinates, the data, and the current stage of the path as arguments. It yields each solution as it finds them.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def recursive_check(x,y,data,stage):\n",
|
|
" # foreach direction check if within bounds\n",
|
|
" for x,y in [(x,y) for x,y in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)] if x>=0 and x < len(data) and y>=0 and y < len(data[0])]:\n",
|
|
" # if in bounds and on correct hight\n",
|
|
" if int(data[x][y]) == stage:\n",
|
|
" # if on last stage return solution\n",
|
|
" if stage == 9:\n",
|
|
" yield (x,y) \n",
|
|
" else: \n",
|
|
" # return all solutions from next stage\n",
|
|
" for solution in recursive_check(x,y,data,stage+1):\n",
|
|
" yield solution\n",
|
|
" else: \n",
|
|
" continue"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Solution for 10 a\n",
|
|
"\n",
|
|
"counting the number of unique peaks for each zero (start)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"841\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"summ = 0\n",
|
|
"for zero in find_zeros(data):\n",
|
|
" peaks = set(recursive_check(zero[0],zero[1],data,1))\n",
|
|
" summ += len(peaks) \n",
|
|
"\n",
|
|
"print(summ)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Solution for 10 b\n",
|
|
"\n",
|
|
"counting the number of unique paths to a peak (9) for each zero (start)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1875\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"summ = 0\n",
|
|
"for zero in find_zeros(data):\n",
|
|
" peaks = list(recursive_check(zero[0],zero[1],data,1))\n",
|
|
" summ += len(peaks) \n",
|
|
"\n",
|
|
"print(summ)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "advent",
|
|
"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.13.0"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|