177 lines
4.8 KiB
Plaintext
177 lines
4.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "aa9a2307-0fb5-4993-a0d9-35e6528a1c0f",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Puzzle 2 of AoC 2025\n",
|
|
"Author: Victoria Ramírez López\n",
|
|
"\n",
|
|
"Date: Dec 4, 2025"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"id": "051c8d59-04e3-4349-b029-32a76466583c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from scipy.signal import convolve2d"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 110,
|
|
"id": "f40bd11f-5e02-4833-a0ea-17d6474f21c8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Read input file\n",
|
|
"\n",
|
|
"paper_roll_grid = open('paper_roll_grid.txt', mode ='r') # Open in 'read only' mode\n",
|
|
"grid = paper_roll_grid.readlines()\n",
|
|
"grid = [line.strip('\\n') for line in grid]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 111,
|
|
"id": "cd2ef290-5e35-4b12-b3f0-87018f2b30b6",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Part 1 answer: 1346\n",
|
|
"Part 2 answer: 8493\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Find roll papers accessible by fork lift\n",
|
|
"\n",
|
|
"total_accessible_rolls = 1\n",
|
|
"total_removed_rolls = 0\n",
|
|
"\n",
|
|
" # Turn grid to boolean matrix \n",
|
|
"bool_grid = create_boolean_grid(grid)\n",
|
|
"bool_grid_np = np.array(bool_grid) # Convert into numpy array\n",
|
|
"\n",
|
|
"# For part 1\n",
|
|
"accessible_rolls = find_accessible_rolls(bool_grid_np)\n",
|
|
"total_accessible_rolls = accessible_rolls.sum()\n",
|
|
"print('Part 1 answer: ', total_accessible_rolls)\n",
|
|
"\n",
|
|
"# For part 2\n",
|
|
"while total_accessible_rolls >= 1:\n",
|
|
" accessible_rolls = find_accessible_rolls(bool_grid_np)\n",
|
|
" total_accessible_rolls = accessible_rolls.sum()\n",
|
|
" bool_grid_np = remove_accessible_rolls(bool_grid_np,accessible_rolls)\n",
|
|
" total_removed_rolls = total_removed_rolls + total_accessible_rolls\n",
|
|
" \n",
|
|
"print('Part 2 answer:', total_removed_rolls)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 98,
|
|
"id": "f5966994-e969-4723-8c50-3a26e6bd424d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def find_accessible_rolls(bool_grid_np):\n",
|
|
" # This function finds the number of rolls that can be \n",
|
|
" # accessed by forklift\n",
|
|
" # input: grid = [[String]]\n",
|
|
" # output: sum_accessible_rolls = int\n",
|
|
"\n",
|
|
" # Add padding\n",
|
|
" bool_grid_padded = np.pad(bool_grid_np, 1, mode='constant') \n",
|
|
"\n",
|
|
" # Calculate how many rolls surround each roll\n",
|
|
" convolution = convolve2d(bool_grid_padded, \n",
|
|
" [[1,1,1],[1,0,1],[1,1,1]], mode='same')\n",
|
|
" convolution = convolution[1:-1,1:-1] # Remove padding\n",
|
|
"\n",
|
|
" # Determine how many rolls are reachable\n",
|
|
" accessible_rolls = bool_grid_np & (convolution < 4)\n",
|
|
"\n",
|
|
" return accessible_rolls"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 109,
|
|
"id": "a62faf9d-2c86-474f-a403-7264129b6c42",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def remove_accessible_rolls(bool_grid_np, accessible_rolls):\n",
|
|
" # This function removes the number of rolls that can be \n",
|
|
" # accessed by forklift\n",
|
|
" # input: accessible_rolls = numpy array\n",
|
|
" # output: updated_grid = numpy array\n",
|
|
" \n",
|
|
" updated_grid = np.logical_not(accessible_rolls) & bool_grid_np\n",
|
|
"\n",
|
|
" return(updated_grid) "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 112,
|
|
"id": "2b34b18d-6d8f-40fb-b619-502ec1b0a346",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def create_boolean_grid(grid):\n",
|
|
" # This function turns the original grid to a matrix where\n",
|
|
" # '@' is 1 and '.' is 0\n",
|
|
" # input: grid = [String]\n",
|
|
" # output: boolean_grid = [int]\n",
|
|
" \n",
|
|
" boolean_grid = []\n",
|
|
" \n",
|
|
" for row in grid:\n",
|
|
" boolean_row = [1 if x == '@' else 0 for x in row]\n",
|
|
" boolean_grid.append(boolean_row)\n",
|
|
"\n",
|
|
" return boolean_grid"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "77ebe3d0-11bb-4553-ae88-666f704357c0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"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.10.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|