AdventOfCode/Python/day1/solution.ipynb

132 lines
2.3 KiB
Text
Raw Permalink Normal View History

2020-11-22 14:49:21 +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": 10,
"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": 11,
"metadata": {},
"outputs": [],
"source": [
"ups = puzzle_input.count(\"(\")\n",
"downs = puzzle_input.count(\")\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"end_floor = ups - downs"
]
},
{
"source": [
"print(end_floor)"
],
"cell_type": "code",
"metadata": {},
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"138\n"
]
}
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"current_floor = 0"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"index = 0\n",
"while current_floor >= 0 and index < len(puzzle_input):\n",
" if puzzle_input[index] == \"(\":\n",
" current_floor += 1\n",
" elif puzzle_input[index] == \")\":\n",
" current_floor -= 1\n",
" index += 1"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1771\n"
]
}
],
"source": [
"print(index)"
]
}
]
}