{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advent of Code - Day 9: Encoding Error\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part One\n",
"\n",
"With your neighbor happily enjoying their video game, you turn your attention to\n",
"an open data port on the little screen in the seat in front of you.\n",
"\n",
"Though the port is non-standard, you manage to connect it to your computer\n",
"through the clever use of several paperclips. Upon connection, the port outputs\n",
"a series of numbers (your puzzle input).\n",
"\n",
"The data appears to be encrypted with the eXchange-Masking Addition System\n",
"(XMAS) which, conveniently for you, is an old cypher with an important weakness.\n",
"\n",
"XMAS starts by transmitting a preamble of 25 numbers. After that, each number\n",
"you receive should be the sum of any two of the 25 immediately previous numbers.\n",
"The two numbers will have different values, and there might be more than one\n",
"such pair.\n",
"\n",
"For example, suppose your preamble consists of the numbers 1 through 25 in a\n",
"random order. To be valid, the next number must be the sum of two of those\n",
"numbers:\n",
"\n",
"- 26 would be a valid next number, as it could be 1 plus 25 (or many other\n",
" pairs, like 2 and 24).\n",
"- 49 would be a valid next number, as it is the sum of 24 and 25.\n",
"- 100 would not be valid; no two of the previous 25 numbers sum to 100.\n",
"- 50 would also not be valid; although 25 appears in the previous 25 numbers,\n",
" the two numbers in the pair must be different.\n",
"\n",
"Suppose the 26th number is 45, and the first number (no longer an option, as it\n",
"is more than 25 numbers ago) was 20. Now, for the next number to be valid, there\n",
"needs to be some pair of numbers among 1-19, 21-25, or 45 that add up to it:\n",
"\n",
"- 26 would still be a valid next number, as 1 and 25 are still within the\n",
" previous 25 numbers.\n",
"- 65 would not be valid, as no two of the available numbers sum to it.\n",
"- 64 and 66 would both be valid, as they are the result of 19+45 and 21+45\n",
" respectively.\n",
"\n",
"Here is a larger example which only considers the previous 5 numbers (and has a\n",
"preamble of length 5):\n",
"\n",
"```\n",
"35\n",
"20\n",
"15\n",
"25\n",
"47\n",
"40\n",
"62\n",
"55\n",
"65\n",
"95\n",
"102\n",
"117\n",
"150\n",
"182\n",
"127\n",
"219\n",
"299\n",
"277\n",
"309\n",
"576\n",
"```\n",
"\n",
"In this example, after the 5-number preamble, almost every number is the sum of\n",
"two of the previous 5 numbers; the only number that does not follow this rule\n",
"is 127.\n",
"\n",
"The first step of attacking the weakness in the XMAS data is to find the first\n",
"number in the list (after the preamble) which is not the sum of two of the 25\n",
"numbers before it. **What is the first number that does not have this\n",
"property?**\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load and Clean Input data\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
":::{note} The input data can be found\n",
"[here](https://adventofcode.com/2020/day/9). :::\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"../../data/advent-of-code/2020/day-9-input\") as fid:\n",
" data = fid.readlines()\n",
" data = [int(x) for x in data]\n",
"\n",
"print(len(data))\n",
"print(data[0:2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_data = [\n",
" 35,\n",
" 20,\n",
" 15,\n",
" 25,\n",
" 47,\n",
" 40,\n",
" 62,\n",
" 55,\n",
" 65,\n",
" 95,\n",
" 102,\n",
" 117,\n",
" 150,\n",
" 182,\n",
" 127,\n",
" 219,\n",
" 299,\n",
" 277,\n",
" 309,\n",
" 576,\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def pairs_addup(data, target):\n",
" n1, n2 = None, None\n",
" for idx, num in enumerate(data):\n",
" remain = target - num\n",
" if remain in data[:idx] + data[idx + 1 :]:\n",
" n1, n2 = num, remain\n",
" break\n",
" return n1, n2\n",
"\n",
"\n",
"def find_invalid_number(data, preamble_length, return_index=False):\n",
" arr = data[preamble_length:]\n",
" for idx, num in enumerate(arr):\n",
" start, stop = idx, idx + preamble_length\n",
" arr_to_check = data[start:stop]\n",
" n1, n2 = pairs_addup(arr_to_check, num)\n",
" if n1 is None:\n",
" if return_index:\n",
" return num, stop\n",
" else:\n",
" return num\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"127 is the number that does not have the required property\n"
]
}
],
"source": [
"print(f\"{find_invalid_number(test_data, 5)} is the number that does not have the required property\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25918798 is the number that does not have the required property\n"
]
}
],
"source": [
"print(f\"{find_invalid_number(data, 25)} is the number that does not have the required property\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part Two\n",
"\n",
"The final step in breaking the XMAS encryption relies on the invalid number you\n",
"just found: you must find a contiguous set of at least two numbers in your list\n",
"which sum to the invalid number from step 1.\n",
"\n",
"Again consider the above example:\n",
"\n",
"```\n",
"35\n",
"20\n",
"15\n",
"25\n",
"47\n",
"40\n",
"62\n",
"55\n",
"65\n",
"95\n",
"102\n",
"117\n",
"150\n",
"182\n",
"127\n",
"219\n",
"299\n",
"277\n",
"309\n",
"576\n",
"```\n",
"\n",
"In this list, adding up all of the numbers from 15 through 40 produces the\n",
"invalid number from step 1, 127. (Of course, the contiguous set of numbers in\n",
"your actual list might be much longer.)\n",
"\n",
"To find the encryption weakness, add together the smallest and largest number in\n",
"this contiguous range; in this example, these are 15 and 47, producing 62.\n",
"\n",
"**What is the encryption weakness in your XMAS-encrypted list of numbers?**\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25918798"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num = find_invalid_number(data, 25)\n",
"num"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def find_contiguous_block(data, target):\n",
" count = 0\n",
" length = len(data)\n",
" # Check each size from second index all the way up to the end of the array\n",
" for idx in range(2, length):\n",
" # At each size, shift the window from front to back\n",
" for i in range(0, length - idx + 1):\n",
" total = 0\n",
" candidates = []\n",
" # Add numbers in the computed window and check if they sum to the target\n",
" for j in range(0, idx):\n",
" total += data[i + j]\n",
" candidates.append(data[i + j])\n",
"\n",
" # Does the total equal to the target?\n",
" if total == target:\n",
" min_num, max_num = min(candidates), max(candidates)\n",
" return min_num, max_num"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(15, 47)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"find_contiguous_block(test_data, 127)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1127699, 2213243)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min_num, max_num = find_contiguous_block(data, num)\n",
"min_num, max_num"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The the encryption weakness in our XMAS-encrypted list of numbers is 3340942\n"
]
}
],
"source": [
"print(f\"The the encryption weakness in our XMAS-encrypted list of numbers is {min_num + max_num}\")"
]
}
],
"metadata": {
"author": "Anderson Banihirwe",
"date": "2020-12-09",
"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 9: Encoding Error",
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}