adventofcode2020/day04.ipynb

138 lines
3.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"222\n"
]
}
],
"source": [
"# part 01\n",
"\n",
"input = open('day04-input.txt')\n",
"passport = set()\n",
"valid_passport = 0\n",
"\n",
"for line in list(input) + ['']:\n",
" if line.strip() == \"\":\n",
" # check that the current stuff is there\n",
" if req_fields.issubset(passport):\n",
" valid_passport +=1\n",
" passport.clear()\n",
" else:\n",
" matches = re.findall(r\"(\\w{3}):[^ ]*\", line)\n",
" for match in matches:\n",
" passport.add(match)\n",
" \n",
"print(valid_passport)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"140\n"
]
}
],
"source": [
"# PART 02\n",
"\n",
"input = open('day04-input.txt')\n",
"passport = set()\n",
"req_fields = {\"byr\", \"iyr\", \"eyr\", \"hgt\", \"hcl\", \"ecl\", \"pid\"}\n",
"eye_colors = [\"amb\", \"blu\", \"brn\", \"gry\", \"grn\", \"hzl\", \"oth\"]\n",
"\n",
"valid_passport = 0\n",
"\n",
"for line in list(input) + ['']:\n",
" line = line.strip()\n",
" if line == \"\":\n",
" # check that the current stuff is there\n",
" if req_fields.issubset(passport):\n",
" valid_passport +=1\n",
" passport.clear()\n",
" else:\n",
" matches = re.findall(r\"(\\w{3}):([^ ]*)\", line)\n",
" for key, value in matches:\n",
" if key == \"byr\" and not 1920 <= int(value) <= 2002:\n",
" continue\n",
" if key == \"iyr\" and not 2010 <= int(value) <= 2020:\n",
" continue\n",
" if key == \"eyr\" and not 2020 <= int(value) <= 2030:\n",
" continue\n",
" if key == \"hgt\":\n",
" if value.endswith(\"cm\"):\n",
" if not 150 <= int(value[:-2]) <= 193:\n",
" continue\n",
" elif value.endswith(\"in\"):\n",
" if not 59 <= int(value[:-2]) <= 76:\n",
" continue\n",
" else:\n",
" continue\n",
" if key == \"hcl\" and not re.match(r\"#[0-9a-f]{6}$\", value):\n",
" continue\n",
" if key == \"ecl\" and value not in eye_colors:\n",
" continue\n",
" if key == \"pid\" and not re.match(r\"\\d{9}$\", value):\n",
" continue\n",
" \n",
" passport.add(key)\n",
" \n",
"print(valid_passport)"
]
},
{
"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
}