finish day 2 problems
This commit is contained in:
parent
517e93c440
commit
9543a4582c
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,109 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import re"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"640\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"passwords = open('day02-input.txt')\n",
|
||||
"\n",
|
||||
"valid_pass = 0\n",
|
||||
"\n",
|
||||
"for passw in passwords:\n",
|
||||
" policy = re.match(r\"(\\d+)-(\\d+) (.): (.*)\", passw).groups()\n",
|
||||
" min = int(policy[0])\n",
|
||||
" max = int(policy[1])\n",
|
||||
" pass_char = policy[2]\n",
|
||||
" password = policy[3]\n",
|
||||
" counter = 0\n",
|
||||
" for x in password:\n",
|
||||
" if x == pass_char:\n",
|
||||
" counter += 1\n",
|
||||
" if min <= counter <= max:\n",
|
||||
" valid_pass +=1\n",
|
||||
"\n",
|
||||
"print(valid_pass)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 42,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"472\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"passwords = open('day02-input.txt')\n",
|
||||
"\n",
|
||||
"valid_pass = 0\n",
|
||||
"\n",
|
||||
"for passw in passwords:\n",
|
||||
" policy = re.match(r\"(\\d+)-(\\d+) (.): (.*)\", passw).groups()\n",
|
||||
" i1 = int(policy[0])\n",
|
||||
" i2 = int(policy[1])\n",
|
||||
" pass_char = policy[2]\n",
|
||||
" password = policy[3]\n",
|
||||
" if password[i1-1] == pass_char:\n",
|
||||
" if password[i2-1] != pass_char:\n",
|
||||
" valid_pass += 1\n",
|
||||
" else:\n",
|
||||
" if password[i2-1] == pass_char:\n",
|
||||
" valid_pass += 1\n",
|
||||
"\n",
|
||||
"print(valid_pass)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"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.8.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
Loading…
Reference in New Issue