{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advent of Code - Day 4: Passport Processing\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part One\n", "\n", "You arrive at the airport only to realize that you grabbed your North Pole\n", "Credentials instead of your passport. While these documents are extremely\n", "similar, North Pole Credentials aren't issued by a country and therefore aren't\n", "actually valid documentation for travel in most of the world.\n", "\n", "It seems like you're not the only one having problems, though; a very long line\n", "has formed for the automatic passport scanners, and the delay could upset your\n", "travel itinerary.\n", "\n", "Due to some questionable network security, you realize you might be able to\n", "solve both of these problems at the same time.\n", "\n", "The automatic passport scanners are slow because they're having trouble\n", "detecting which passports have all required fields. The expected fields are as\n", "follows:\n", "\n", "- byr (Birth Year)\n", "- iyr (Issue Year)\n", "- eyr (Expiration Year)\n", "- hgt (Height)\n", "- hcl (Hair Color)\n", "- ecl (Eye Color)\n", "- pid (Passport ID)\n", "- cid (Country ID)\n", "\n", "Passport data is validated in batch files (your puzzle input). Each passport is\n", "represented as a sequence of key:value pairs separated by spaces or newlines.\n", "Passports are separated by blank lines.\n", "\n", "Here is an example batch file containing four passports:\n", "\n", "```\n", "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd\n", "byr:1937 iyr:2017 cid:147 hgt:183cm\n", "\n", "iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884\n", "hcl:#cfa07d byr:1929\n", "\n", "hcl:#ae17e1 iyr:2013\n", "eyr:2024\n", "ecl:brn pid:760753108 byr:1931\n", "hgt:179cm\n", "\n", "hcl:#cfa07d eyr:2025 pid:166559648\n", "iyr:2011 ecl:brn hgt:59in\n", "```\n", "\n", "The first passport is valid - all eight fields are present. The second passport\n", "is invalid - it is missing hgt (the Height field).\n", "\n", "The third passport is interesting; the only missing field is cid, so it looks\n", "like data from North Pole Credentials, not a passport at all! Surely, nobody\n", "would mind if you made the system temporarily ignore missing cid fields. Treat\n", "this \"passport\" as valid.\n", "\n", "The fourth passport is missing two fields, cid and byr. Missing cid is fine, but\n", "missing any other field is not, so this passport is invalid.\n", "\n", "According to the above rules, your improved system would report 2 valid\n", "passports.\n", "\n", "Count the number of valid passports - those that have all required fields. Treat\n", "cid as optional. **In your batch file, how many passports are valid?**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load Input data\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":::{note} The input data can be found\n", "[here](https://adventofcode.com/2020/day/4). :::\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "260\n" ] } ], "source": [ "def parse_input_data(data):\n", " passports = data.split(\"\\n\\n\")\n", " passports = [passport.strip().replace(\"\\n\", \" \").split(\" \") for passport in passports]\n", " passports = [dict(map(lambda x: x.split(\":\"), passport)) for passport in passports]\n", " return passports\n", "\n", "\n", "with open(\"../../data/advent-of-code/2020/day-4-input\") as fid:\n", " data = fid.read()\n", " passports = parse_input_data(data)\n", "\n", "print(len(passports))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eyr': '2039',\n", " 'hgt': '64',\n", " 'ecl': '#ab45a8',\n", " 'byr': '2009',\n", " 'iyr': '2025',\n", " 'pid': '182cm',\n", " 'hcl': 'd1614a',\n", " 'cid': '103'}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "passports[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "required_fields = {\"byr\", \"iyr\", \"eyr\", \"hgt\", \"hcl\", \"ecl\", \"pid\"}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'ecl': 'gry',\n", " 'pid': '860033327',\n", " 'eyr': '2020',\n", " 'hcl': '#fffffd',\n", " 'byr': '1937',\n", " 'iyr': '2017',\n", " 'cid': '147',\n", " 'hgt': '183cm'},\n", " {'iyr': '2013',\n", " 'ecl': 'amb',\n", " 'cid': '350',\n", " 'eyr': '2023',\n", " 'pid': '028048884',\n", " 'hcl': '#cfa07d',\n", " 'byr': '1929'}]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_data = parse_input_data(\n", " \"\"\"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd\n", "byr:1937 iyr:2017 cid:147 hgt:183cm\n", "\n", "iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884\n", "hcl:#cfa07d byr:1929\n", "\n", "hcl:#ae17e1 iyr:2013\n", "eyr:2024\n", "ecl:brn pid:760753108 byr:1931\n", "hgt:179cm\n", "\n", "hcl:#cfa07d eyr:2025 pid:166559648\n", "iyr:2011 ecl:brn hgt:59in\"\"\"\n", ")\n", "test_data[0:2]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def is_passport_valid(passport, required_fields=required_fields):\n", " keys = set(passport.keys())\n", " if required_fields.intersection(keys) == required_fields:\n", " return True\n", " return False" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_passport_valid(test_data[0])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_passport_valid(test_data[1])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_passport_valid(test_data[2])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def valid_passport_counter(passports, pass_validator=is_passport_valid):\n", " counter = 0\n", " for passport in passports:\n", " if pass_validator(passport):\n", " counter += 1\n", " return counter" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "valid_passport_counter(test_data)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Out of 260 passports, 222 were found to be valid.\n" ] } ], "source": [ "print(\n", " f\"Out of {len(passports)} passports, {valid_passport_counter(passports)} were found to be valid.\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part Two\n", "\n", "The line is moving more quickly now, but you overhear airport security talking\n", "about how passports with invalid data are getting through. Better add some data\n", "validation, quick!\n", "\n", "You can continue to ignore the cid field, but each other field has strict rules\n", "about what values are valid for automatic validation:\n", "\n", "- byr (Birth Year) - four digits; at least 1920 and at most 2002.\n", "- iyr (Issue Year) - four digits; at least 2010 and at most 2020.\n", "- eyr (Expiration Year) - four digits; at least 2020 and at most 2030.\n", "- hgt (Height) - a number followed by either cm or in:\n", " - If cm, the number must be at least 150 and at most 193.\n", " - If in, the number must be at least 59 and at most 76.\n", "- hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.\n", "- ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.\n", "- pid (Passport ID) - a nine-digit number, including leading zeroes.\n", "- cid (Country ID) - ignored, missing or not.\n", "\n", "Your job is to count the passports where all required fields are both present\n", "and valid according to the above rules. Here are some example values:\n", "\n", "```\n", "byr valid: 2002\n", "byr invalid: 2003\n", "\n", "hgt valid: 60in\n", "hgt valid: 190cm\n", "hgt invalid: 190in\n", "hgt invalid: 190\n", "\n", "hcl valid: #123abc\n", "hcl invalid: #123abz\n", "hcl invalid: 123abc\n", "\n", "ecl valid: brn\n", "ecl invalid: wat\n", "\n", "pid valid: 000000001\n", "pid invalid: 0123456789\n", "```\n", "\n", "Here are some invalid passports:\n", "\n", "```\n", "eyr:1972 cid:100\n", "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926\n", "\n", "iyr:2019\n", "hcl:#602927 eyr:1967 hgt:170cm\n", "ecl:grn pid:012533040 byr:1946\n", "\n", "hcl:dab227 iyr:2012\n", "ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277\n", "\n", "hgt:59cm ecl:zzz\n", "eyr:2038 hcl:74454a iyr:2023\n", "pid:3556412378 byr:2007\n", "```\n", "\n", "Here are some valid passports:\n", "\n", "```\n", "pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980\n", "hcl:#623a2f\n", "\n", "eyr:2029 ecl:blu cid:129 byr:1989\n", "iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm\n", "\n", "hcl:#888785\n", "hgt:164cm byr:2001 iyr:2015 cid:88\n", "pid:545766238 ecl:hzl\n", "eyr:2022\n", "\n", "iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719\n", "\n", "```\n", "\n", "Count the number of valid passports - those that have all required fields and\n", "valid values. Continue to treat cid as optional. In your batch file, **how many\n", "passports are valid?**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def validate_byr(byr):\n", " return 1920 <= int(byr) <= 2002\n", "\n", "\n", "def validate_iyr(iyr):\n", " return 2010 <= int(iyr) <= 2020\n", "\n", "\n", "def validate_eyr(eyr):\n", " return 2020 <= int(eyr) <= 2030\n", "\n", "\n", "def validate_hgt(height):\n", " hgt = int(height[:-2])\n", " hgt_unit = height[-2:]\n", " hgt_validator = False\n", " if hgt_unit == \"in\":\n", " hgt_validator = 59 <= hgt <= 76\n", " elif hgt_unit == \"cm\":\n", " hgt_validator = 150 <= hgt <= 193\n", " else:\n", " hgt_validator = False\n", " return hgt_validator\n", "\n", "\n", "def validate_hcl(hcl):\n", " if not hcl.startswith(\"#\") or len(hcl[1:]) != 6:\n", " return False\n", " elif not set(hcl[1:]).difference(set(list(\"0123456789abcdef\"))):\n", " return True\n", " return False\n", "\n", "\n", "def validate_ecl(ecl):\n", " return ecl in [\"amb\", \"blu\", \"brn\", \"gry\", \"grn\", \"hzl\", \"oth\"]\n", "\n", "\n", "def validate_pid(pid):\n", " return len(pid) == 9 and not set(pid).difference(set(list(\"0123456789\")))\n", "\n", "\n", "def is_passport_valid_enhanced(passport, required_fields=required_fields):\n", " keys = set(passport.keys())\n", " if required_fields.intersection(keys) == required_fields:\n", " try:\n", " return (\n", " validate_byr(passport[\"byr\"])\n", " and validate_iyr(passport[\"iyr\"])\n", " and validate_eyr(passport[\"eyr\"])\n", " and validate_hgt(passport[\"hgt\"])\n", " and validate_hcl(passport[\"hcl\"])\n", " and validate_ecl(passport[\"ecl\"])\n", " and validate_pid(passport[\"pid\"])\n", " )\n", " except Exception:\n", " return False\n", " return False" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 4)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_data_invalid = parse_input_data(\n", " \"\"\"eyr:1972 cid:100\n", "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926\n", "\n", "iyr:2019\n", "hcl:#602927 eyr:1967 hgt:170cm\n", "ecl:grn pid:012533040 byr:1946\n", "\n", "hcl:dab227 iyr:2012\n", "ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277\n", "\n", "hgt:59cm ecl:zzz\n", "eyr:2038 hcl:74454a iyr:2023\n", "pid:3556412378 byr:2007\"\"\"\n", ")\n", "\n", "test_data_valid = parse_input_data(\n", " \"\"\"pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980\n", "hcl:#623a2f\n", "\n", "eyr:2029 ecl:blu cid:129 byr:1989\n", "iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm\n", "\n", "hcl:#888785\n", "hgt:164cm byr:2001 iyr:2015 cid:88\n", "pid:545766238 ecl:hzl\n", "eyr:2022\n", "\n", "iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719\n", "\"\"\"\n", ")\n", "len(test_data_invalid), len(test_data_valid)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_passport_valid_enhanced(test_data_invalid[3])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "valid_passport_counter(test_data_invalid, pass_validator=is_passport_valid_enhanced)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_passport_valid_enhanced(test_data_valid[2])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "valid_passport_counter(test_data_valid, pass_validator=is_passport_valid_enhanced)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Out of 260 passports, 140 were found to be valid.\n" ] } ], "source": [ "print(\n", " f\"Out of {len(passports)} passports, {valid_passport_counter(passports, pass_validator=is_passport_valid_enhanced)} were found to be valid.\"\n", ")" ] } ], "metadata": { "author": "Anderson Banihirwe", "date": "2020-12-04", "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.8" }, "tags": "python,adventofcode", "title": "Advent of Code - Day 4: Passport Processing", "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }