Python Jupyter Day 3

This commit is contained in:
Tobias Berger 2020-11-22 17:15:08 +01:00
parent 3aadfdedd0
commit 19417310a3
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
2 changed files with 124 additions and 0 deletions

1
Python/day3/input Normal file

File diff suppressed because one or more lines are too long

123
Python/day3/solution.ipynb Normal file
View file

@ -0,0 +1,123 @@
{
"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()"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2572\n"
]
}
],
"source": [
"delivered = {(0,0)}\n",
"\n",
"x, y = 0, 0\n",
"ops = {\n",
" \"^\": [+0, +1],\n",
" \"v\": [-0, -1],\n",
" \"<\": [-1, -0],\n",
" \">\": [+1, +0]\n",
"}\n",
"for char in puzzle_input:\n",
" dx, dy = ops[char]\n",
" x += dx\n",
" y += dy\n",
" delivered.add((x, y))\n",
"print(len(delivered))"
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2631\n"
]
}
],
"source": [
"delivered = {(0,0)}\n",
"\n",
"santa = (0, 0)\n",
"robo_santa = (0, 0)\n",
"ops = {\n",
" \"^\": [+0, +1],\n",
" \"v\": [-0, -1],\n",
" \"<\": [-1, -0],\n",
" \">\": [+1, +0]\n",
"}\n",
"robot = False\n",
"for char in puzzle_input:\n",
" dx, dy = ops[char]\n",
" if robot:\n",
" robo_santa = (robo_santa[0]+dx, robo_santa[1]+dy)\n",
" delivered.add(robo_santa)\n",
" else:\n",
" santa = (santa[0]+dx, santa[1]+dy)\n",
" delivered.add(santa)\n",
" robot = not robot\n",
"print(len(delivered))"
]
}
]
}