AdventOfCode/Python/day2/solution.ipynb

104 lines
2.3 KiB
Text
Raw Permalink Normal View History

2020-11-22 16:53:33 +01:00
{
"metadata": {
"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.6-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.6 64-bit",
"metadata": {
"interpreter": {
"hash": "1ddf53fa84779e14f0db18342168679c9c165a81c5f324dac828befab228d68d"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read().split(\"\\n\")"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1606483\n"
]
}
],
"source": [
"wrapping_paper = 0\n",
"for packet in puzzle_input:\n",
" l, w, h = map(lambda val: int(val), packet.split(\"x\"))\n",
" # the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l\n",
" sides = [l*w*2, w*h*2, l*h*2]\n",
" # extra paper for each present: the area of the smallest side.\n",
" wrapping_paper += sum(sides) + int(min(sides)/2)\n",
"print(wrapping_paper)"
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3842356\n"
]
}
],
"source": [
"ribbon = 0\n",
"for packet in puzzle_input:\n",
" l, w, h = map(lambda val: int(val), packet.split(\"x\"))\n",
" # the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present\n",
" ribbon += l*w*h\n",
" # The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face\n",
" x, y = sorted([l, w, h])[0:2]\n",
" ribbon += 2*x + 2*y\n",
"print(ribbon)"
]
}
]
}