AdventOfCode/Python/day4/solution.ipynb

112 lines
No EOL
2 KiB
Text

{
"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": [
"import hashlib"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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": 16,
"metadata": {
"tags": []
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"282749\n"
]
}
],
"source": [
"current_hash = \"\"\n",
"i = 0\n",
"while not current_hash[0:5] == \"00000\":\n",
" i += 1\n",
" md5 = hashlib.md5(f\"{puzzle_input}{i}\".encode(\"utf-8\"))\n",
" current_hash = md5.hexdigest()\n",
"print(i)"
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"9962624\n"
]
}
],
"source": [
"current_hash = \"\"\n",
"i = 0\n",
"while not current_hash[0:6] == \"000000\":\n",
" i += 1\n",
" md5 = hashlib.md5(f\"{puzzle_input}{i}\".encode(\"utf-8\"))\n",
" current_hash = md5.hexdigest()\n",
"print(i)"
]
}
]
}