{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advent of Code - Day 6: Custom Customs\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part One\n", "\n", "As your flight approaches the regional airport where you'll switch to a much\n", "larger plane,\n", "[customs declaration forms](https://en.wikipedia.org/wiki/Customs_declaration)\n", "are distributed to the passengers.\n", "\n", "The form asks a series of 26 yes-or-no questions marked a through z. All you\n", "need to do is identify the questions for which anyone in your group answers\n", "\"yes\". Since your group is just you, this doesn't take very long.\n", "\n", "However, the person sitting next to you seems to be experiencing a language\n", "barrier and asks if you can help. For each of the people in their group, you\n", "write down the questions for which they answer \"yes\", one per line. For example:\n", "\n", "```\n", "abcx\n", "abcy\n", "abcz\n", "```\n", "\n", "In this group, there are 6 questions to which anyone answered \"yes\": a, b, c, x,\n", "y, and z. (Duplicate answers to the same question don't count extra; each\n", "question counts at most once.)\n", "\n", "Another group asks for your help, then another, and eventually you've collected\n", "answers from every group on the plane (your puzzle input). Each group's answers\n", "are separated by a blank line, and within each group, each person's answers are\n", "on a single line. For example:\n", "\n", "```\n", "abc\n", "\n", "a\n", "b\n", "c\n", "\n", "ab\n", "ac\n", "\n", "a\n", "a\n", "a\n", "a\n", "\n", "b\n", "\n", "```\n", "\n", "This list represents answers from five groups:\n", "\n", "- The first group contains one person who answered \"yes\" to 3 questions: a, b,\n", " and c.\n", "- The second group contains three people; combined, they answered \"yes\" to 3\n", " questions: a, b, and c.\n", "- The third group contains two people; combined, they answered \"yes\" to 3\n", " questions: a, b, and c.\n", "- The fourth group contains four people; combined, they answered \"yes\" to only 1\n", " question, a.\n", "- The last group contains one person who answered \"yes\" to only 1 question, b.\n", "\n", "In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11.\n", "\n", "For each group, count the number of questions to which anyone answered \"yes\".\n", "**What is the sum of those counts?**\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/6). :::\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "461\n", "[['b', 'b', 'b', 'b'], ['x', 'xfkj', 'xb'], ['ovuxdgiheszjbaltw', 'oxwjiubhfylzavst'], ['se', 'u', 'j', 'se'], ['eaxzstqkujdlhi', 'dsinkoqhjxz']]\n" ] } ], "source": [ "def parse_input_data(data):\n", " data = data.split(\"\\n\\n\")\n", " return [x.strip().split() for x in data]\n", "\n", "\n", "with open(\"../../data/advent-of-code/2020/day-6-input\") as fid:\n", " data = fid.read()\n", " data = parse_input_data(data)\n", "\n", "print(len(data))\n", "print(data[0:5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def counter(group):\n", " return len(set(\"\".join(group)))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['abc'], ['a', 'b', 'c'], ['ab', 'ac'], ['a', 'a', 'a', 'a'], ['b']]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_sample = parse_input_data(\n", " \"\"\"abc\n", "\n", "a\n", "b\n", "c\n", "\n", "ab\n", "ac\n", "\n", "a\n", "a\n", "a\n", "a\n", "\n", "b\"\"\"\n", ")\n", "test_sample" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 3, 3, 1, 1]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[counter(group) for group in test_sample]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6534" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(counter(group) for group in data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part Two\n", "\n", "As you finish the last group's customs declaration, you notice that you misread\n", "one word in the instructions:\n", "\n", "You don't need to identify the questions to which anyone answered \"yes\"; you\n", "need to identify the questions to which everyone answered \"yes\"!\n", "\n", "Using the same example as above:\n", "\n", "```\n", "abc\n", "\n", "a\n", "b\n", "c\n", "\n", "ab\n", "ac\n", "\n", "a\n", "a\n", "a\n", "a\n", "\n", "b\n", "\n", "```\n", "\n", "This list represents answers from five groups:\n", "\n", "- In the first group, everyone (all 1 person) answered \"yes\" to 3 questions: a,\n", " b, and c.\n", "- In the second group, there is no question to which everyone answered \"yes\".\n", "- In the third group, everyone answered yes to only 1 question, a. Since some\n", " people did not answer \"yes\" to b or c, they don't count.\n", "- In the fourth group, everyone answered yes to only 1 question, a.\n", "- In the fifth group, everyone (all 1 person) answered \"yes\" to 1 question, b.\n", "\n", "In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.\n", "\n", "For each group, count the number of questions to which everyone answered \"yes\".\n", "**What is the sum of those counts?**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def counter(group):\n", " answers = set(\"\".join(group))\n", " inter = set(group[0])\n", " for entry in group[1:]:\n", " inter = inter.intersection(set(entry))\n", " return len(inter)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 0, 1, 1, 1]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[counter(group) for group in test_sample]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3402" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(counter(group) for group in data)" ] } ], "metadata": { "author": "Anderson Banihirwe", "date": "2020-12-06", "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 6: Custom Customs", "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }