aoc2025/vicky/dec3/dec3.ipynb

138 lines
4.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "dbfb3e8e-42b3-4272-975f-65ffaa80f9e3",
"metadata": {},
"source": [
"# Puzzle 2 of AoC 2025\n",
"Author: Victoria Ramírez López\n",
"\n",
"Date: Dec 3, 2025"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "f96768d1-57d4-4244-b910-3dddd0a7b023",
"metadata": {},
"outputs": [],
"source": [
"# Read input file\n",
"\n",
"battery_banks = open('battery_banks.txt', mode ='r') # Open in 'read only' mode"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "aec9c7ad-f752-4aa9-8a01-c102b599dd8e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[99, 88, 75, 77, 93, 89, 65, 99, 98, 99, 98, 99, 76, 86, 77, 74, 96, 89, 63, 89, 87, 98, 88, 77, 98, 77, 99, 97, 99, 99, 66, 99, 98, 96, 94, 98, 99, 99, 76, 99, 75, 88, 99, 86, 98, 86, 65, 87, 88, 99, 88, 87, 97, 96, 55, 99, 98, 99, 98, 99, 86, 95, 98, 66, 99, 89, 99, 97, 99, 77, 99, 86, 66, 77, 99, 99, 87, 88, 54, 99, 87, 97, 98, 99, 75, 77, 99, 98, 77, 77, 99, 99, 98, 77, 99, 66, 76, 97, 99, 88, 98, 98, 77, 99, 76, 99, 77, 64, 77, 88, 99, 88, 88, 86, 87, 75, 76, 44, 98, 98, 75, 99, 88, 33, 99, 99, 88, 97, 94, 99, 98, 87, 99, 88, 86, 99, 76, 87, 65, 96, 98, 99, 87, 99, 99, 87, 88, 99, 75, 94, 95, 98, 77, 99, 89, 99, 97, 97, 76, 94, 76, 94, 76, 87, 99, 77, 86, 95, 75, 98, 59, 79, 96, 99, 96, 86, 88, 98, 96, 98, 99, 87, 75, 88, 99, 98, 87, 98, 95, 99, 99, 97, 77, 97, 87, 98, 88, 86, 66, 98]\n",
"17766\n"
]
}
],
"source": [
"# Select 2 batteries per bank to turn on\n",
"\n",
"max_joltages = []\n",
"\n",
"for bank in battery_banks:\n",
" bank_string = bank.strip('\\n')\n",
" bank_joltage = calculate_max_joltage(battery_list[0]) # For part 1\n",
" max_joltages.append(bank_joltage)\n",
"\n",
"sum_joltages = sum(max_joltages)\n",
"print(max_joltages)\n",
"print(sum_joltages)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "3c69aa03-9fc9-4c55-9b85-13e4f06f7b18",
"metadata": {},
"outputs": [],
"source": [
"def calculate_max_joltage(bank = ''):\n",
" # This function calculates the max joltage \n",
" # that can be achieved in a battery bank\n",
" # when turning on 2 batteries\n",
" # input: bank = String\n",
" # output: max_joltage = int\n",
"\n",
" # Create a list containing all the batteries in the bank\n",
" battery_list = [int(battery) for battery in bank_string]\n",
"\n",
" # Find the battery with the highest number\n",
" max_battery = max(battery_list[:-1])\n",
" max_position = battery_list.index(max_battery)\n",
"\n",
" if max_position == len(battery_list) - 1:\n",
" max_joltage = (battery_list[max_position] * 10) + battery_list[-1]\n",
" else:\n",
" second_max_battery = max(battery_list[max_position + 1:])\n",
" second_max_position = battery_list.index(second_max_battery)\n",
" max_joltage = (battery_list[max_position] * 10) + battery_list[second_max_position]\n",
"\n",
" return max_joltage"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "59b1fd8b-6aff-408c-b0bb-aa1e55410914",
"metadata": {},
"outputs": [],
"source": [
"## Unfinished start of part 2\n",
"\n",
"def calculate_max_joltage_12(bank = ''):\n",
" # This function calculates the max joltage\n",
" # that can be achieved with a battery bank \n",
" # when turning on 12 batteries\n",
"\n",
" # Create a list containing all the batteries in the bank\n",
" battery_list = [int(battery) for battery in bank_string]\n",
"\n",
" # Find the battery with the highest number\n",
" max_battery = max(battery_list[:-11])\n",
" max_position = battery_list.index(max_battery)\n",
"\n",
" if max_position == len(battery_list) - 1:\n",
" max_joltage_list = [battery_list[i] * 10**(-i-1) for i in range(-12,0)]\n",
" max_joltage = sum(max_joltage_list)\n",
" else:\n",
" for battery in battery_list(\n"
]
}
],
"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
}