-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I like how succint this first part became, then I was lost in the convoluted description of part two...
- Loading branch information
Showing
1 changed file
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Day 15: Lens Library\n", | ||
"\n", | ||
"[*Advent of Code 2023 day 15*](https://adventofcode.com/2023/day/15) and [*solution megathread*](https://redd.it/...)\n", | ||
"\n", | ||
"[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/UncleCJ/advent-of-code/blob/cj/2023/15/code.ipynb) [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/UncleCJ/advent-of-code/cj?filepath=2023%2F15%2Fcode.ipynb)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from IPython.display import HTML\n", | ||
"import sys\n", | ||
"sys.path.append('../../')\n", | ||
"\n", | ||
"\n", | ||
"# %load_ext nb_mypy\n", | ||
"# %nb_mypy On" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Writing 'downloaded' (dict) to file 'downloaded'.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import common\n", | ||
"\n", | ||
"\n", | ||
"downloaded = common.refresh()\n", | ||
"%store downloaded >downloaded\n", | ||
"\n", | ||
"# %load_ext pycodestyle_magic\n", | ||
"# %pycodestyle_on" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<h2>--- Day 15: Lens Library ---</h2><p>The newly-focused parabolic reflector dish is sending all of the collected light to a point on the side of yet another mountain - the largest mountain on Lava Island. As you approach the mountain, you find that the light is being collected by the wall of a large facility embedded in the mountainside.</p>\n", | ||
"<p>You find a door under a large sign that says \"Lava Production Facility\" and next to a smaller sign that says \"Danger - Personal Protective Equipment required beyond this point\".</p>\n", | ||
"<p>As you step inside, you are immediately greeted by a somewhat panicked <span title=\"do you like my hard hat\">reindeer</span> wearing goggles and a loose-fitting <a href=\"https://en.wikipedia.org/wiki/Hard_hat\" target=\"_blank\">hard hat</a>. The reindeer leads you to a shelf of goggles and hard hats (you quickly find some that fit) and then further into the facility. At one point, you pass a button with a faint snout mark and the label \"PUSH FOR HELP\". No wonder you were loaded into that <a href=\"1\">trebuchet</a> so quickly!</p>\n", | ||
"<p>You pass through a final set of doors surrounded with even more warning signs and into what must be the room that collects all of the light from outside. As you admire the large assortment of lenses available to further focus the light, the reindeer brings you a book titled \"Initialization Manual\".</p>\n", | ||
"<p>\"Hello!\", the book cheerfully begins, apparently unaware of the concerned reindeer reading over your shoulder. \"This procedure will let you bring the Lava Production Facility online - all without burning or melting anything unintended!\"</p>\n", | ||
"<p>\"Before you begin, please be prepared to use the Holiday ASCII String Helper algorithm (appendix 1A).\" You turn to appendix 1A. The reindeer leans closer with interest.</p>\n", | ||
"<p>The HASH algorithm is a way to turn any <a href=\"https://en.wikipedia.org/wiki/String_(computer_science)\" target=\"_blank\">string</a> of characters into a single <em>number</em> in the range 0 to 255. To run the HASH algorithm on a string, start with a <em>current value</em> of <code>0</code>. Then, for each character in the string starting from the beginning:</p>\n", | ||
"<ul>\n", | ||
"<li>Determine the <a href=\"https://en.wikipedia.org/wiki/ASCII#Printable_characters\" target=\"_blank\">ASCII code</a> for the current character of the string.</li>\n", | ||
"<li>Increase the <em>current value</em> by the ASCII code you just determined.</li>\n", | ||
"<li>Set the <em>current value</em> to itself multiplied by <code>17</code>.</li>\n", | ||
"<li>Set the <em>current value</em> to the <a href=\"https://en.wikipedia.org/wiki/Modulo\" target=\"_blank\">remainder</a> of dividing itself by <code>256</code>.</li>\n", | ||
"</ul>\n", | ||
"<p>After following these steps for each character in the string in order, the <em>current value</em> is the output of the HASH algorithm.</p>\n", | ||
"<p>So, to find the result of running the HASH algorithm on the string <code>HASH</code>:</p>\n", | ||
"<ul>\n", | ||
"<li>The <em>current value</em> starts at <code>0</code>.</li>\n", | ||
"<li>The first character is <code>H</code>; its ASCII code is <code>72</code>.</li>\n", | ||
"<li>The <em>current value</em> increases to <code>72</code>.</li>\n", | ||
"<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>1224</code>.</li>\n", | ||
"<li>The <em>current value</em> becomes <code><em>200</em></code> (the remainder of <code>1224</code> divided by <code>256</code>).</li>\n", | ||
"<li>The next character is <code>A</code>; its ASCII code is <code>65</code>.</li>\n", | ||
"<li>The <em>current value</em> increases to <code>265</code>.</li>\n", | ||
"<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>4505</code>.</li>\n", | ||
"<li>The <em>current value</em> becomes <code><em>153</em></code> (the remainder of <code>4505</code> divided by <code>256</code>).</li>\n", | ||
"<li>The next character is <code>S</code>; its ASCII code is <code>83</code>.</li>\n", | ||
"<li>The <em>current value</em> increases to <code>236</code>.</li>\n", | ||
"<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>4012</code>.</li>\n", | ||
"<li>The <em>current value</em> becomes <code><em>172</em></code> (the remainder of <code>4012</code> divided by <code>256</code>).</li>\n", | ||
"<li>The next character is <code>H</code>; its ASCII code is <code>72</code>.</li>\n", | ||
"<li>The <em>current value</em> increases to <code>244</code>.</li>\n", | ||
"<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>4148</code>.</li>\n", | ||
"<li>The <em>current value</em> becomes <code><em>52</em></code> (the remainder of <code>4148</code> divided by <code>256</code>).</li>\n", | ||
"</ul>\n", | ||
"<p>So, the result of running the HASH algorithm on the string <code>HASH</code> is <code><em>52</em></code>.</p>\n", | ||
"<p>The <em>initialization sequence</em> (your puzzle input) is a comma-separated list of steps to start the Lava Production Facility. <em>Ignore newline characters</em> when parsing the initialization sequence. To verify that your HASH algorithm is working, the book offers the sum of the result of running the HASH algorithm on each step in the initialization sequence.</p>\n", | ||
"<p>For example:</p>\n", | ||
"<pre><code>rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7</code></pre>\n", | ||
"<p>This initialization sequence specifies 11 individual steps; the result of running the HASH algorithm on each of the steps is as follows:</p>\n", | ||
"<ul>\n", | ||
"<li><code>rn=1</code> becomes <code><em>30</em></code>.</li>\n", | ||
"<li><code>cm-</code> becomes <code><em>253</em></code>.</li>\n", | ||
"<li><code>qp=3</code> becomes <code><em>97</em></code>.</li>\n", | ||
"<li><code>cm=2</code> becomes <code><em>47</em></code>.</li>\n", | ||
"<li><code>qp-</code> becomes <code><em>14</em></code>.</li>\n", | ||
"<li><code>pc=4</code> becomes <code><em>180</em></code>.</li>\n", | ||
"<li><code>ot=9</code> becomes <code><em>9</em></code>.</li>\n", | ||
"<li><code>ab=5</code> becomes <code><em>197</em></code>.</li>\n", | ||
"<li><code>pc-</code> becomes <code><em>48</em></code>.</li>\n", | ||
"<li><code>pc=6</code> becomes <code><em>214</em></code>.</li>\n", | ||
"<li><code>ot=7</code> becomes <code><em>231</em></code>.</li>\n", | ||
"</ul>\n", | ||
"<p>In this example, the sum of these results is <code><em>1320</em></code>. Unfortunately, the reindeer has stolen the page containing the expected verification number and is currently running around the facility with it excitedly.</p>\n", | ||
"<p>Run the HASH algorithm on each step in the initialization sequence. <em>What is the sum of the results?</em> (The initialization sequence is one long line; be careful when copy-pasting it.)</p>\n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from IPython.display import HTML\n", | ||
"\n", | ||
"HTML(downloaded['part1'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"part1_example_input = 'rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"52" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"message = 'HASH'\n", | ||
"def HASH(message):\n", | ||
" current_value = 0\n", | ||
" for character in message:\n", | ||
" current_value += ord(character)\n", | ||
" current_value *= 17\n", | ||
" current_value &= 0xFF\n", | ||
" return current_value\n", | ||
"\n", | ||
"HASH(message)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1320" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"sum(HASH(message) for message in part1_example_input.split(','))\n", | ||
"# sum(HASH(message) for message in downloaded['input'].split(','))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<h2 id=\"part2\">--- Part Two ---</h2><p>You convince the reindeer to bring you the page; the page confirms that your HASH algorithm is working.</p>\n", | ||
"<p>The book goes on to describe a series of 256 <em>boxes</em> numbered <code>0</code> through <code>255</code>. The boxes are arranged in a line starting from the point where light enters the facility. The boxes have holes that allow light to pass from one box to the next all the way down the line.</p>\n", | ||
"<pre><code> +-----+ +-----+ +-----+\n", | ||
"Light | Box | | Box | ... | Box |\n", | ||
"----------------------------------------->\n", | ||
" | 0 | | 1 | ... | 255 |\n", | ||
" +-----+ +-----+ +-----+\n", | ||
"</code></pre>\n", | ||
"<p>Inside each box, there are several <em>lens slots</em> that will keep a lens correctly positioned to focus light passing through the box. The side of each box has a panel that opens to allow you to insert or remove lenses as necessary.</p>\n", | ||
"<p>Along the wall running parallel to the boxes is a large library containing lenses organized by <em>focal length</em> ranging from <code>1</code> through <code>9</code>. The reindeer also brings you a small handheld <a href=\"https://en.wikipedia.org/wiki/Label_printer\" target=\"_blank\">label printer</a>.</p>\n", | ||
"<p>The book goes on to explain how to perform each step in the initialization sequence, a process it calls the Holiday ASCII String Helper Manual Arrangement Procedure, or <em>HASHMAP</em> for short.</p>\n", | ||
"<p>Each step begins with a sequence of letters that indicate the <em>label</em> of the lens on which the step operates. The result of running the HASH algorithm on the label indicates the correct box for that step.</p>\n", | ||
"<p>The label will be immediately followed by a character that indicates the <em>operation</em> to perform: either an equals sign (<code>=</code>) or a dash (<code>-</code>).</p>\n", | ||
"<p>If the operation character is a <em>dash</em> (<code>-</code>), go to the relevant box and remove the lens with the given label if it is present in the box. Then, move any remaining lenses as far forward in the box as they can go without changing their order, filling any space made by removing the indicated lens. (If no lens in that box has the given label, nothing happens.)</p>\n", | ||
"<p>If the operation character is an <em>equals sign</em> (<code>=</code>), it will be followed by a number indicating the <em>focal length</em> of the lens that needs to go into the relevant box; be sure to use the label maker to mark the lens with the label given in the beginning of the step so you can find it later. There are two possible situations:</p>\n", | ||
"<ul>\n", | ||
"<li>If there is already a lens in the box with the same label, <em>replace the old lens</em> with the new lens: remove the old lens and put the new lens in its place, not moving any other lenses in the box.</li>\n", | ||
"<li>If there is <em>not</em> already a lens in the box with the same label, add the lens to the box immediately behind any lenses already in the box. Don't move any of the other lenses when you do this. If there aren't any lenses in the box, the new lens goes all the way to the front of the box.</li>\n", | ||
"</ul>\n", | ||
"<p>Here is the contents of every box after each step in the example initialization sequence above:</p>\n", | ||
"<pre><code>After \"rn=1\":\n", | ||
"Box 0: [rn 1]\n", | ||
"\n", | ||
"After \"cm-\":\n", | ||
"Box 0: [rn 1]\n", | ||
"\n", | ||
"After \"qp=3\":\n", | ||
"Box 0: [rn 1]\n", | ||
"Box 1: [qp 3]\n", | ||
"\n", | ||
"After \"cm=2\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 1: [qp 3]\n", | ||
"\n", | ||
"After \"qp-\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"\n", | ||
"After \"pc=4\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 3: [pc 4]\n", | ||
"\n", | ||
"After \"ot=9\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 3: [pc 4] [ot 9]\n", | ||
"\n", | ||
"After \"ab=5\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 3: [pc 4] [ot 9] [ab 5]\n", | ||
"\n", | ||
"After \"pc-\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 3: [ot 9] [ab 5]\n", | ||
"\n", | ||
"After \"pc=6\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 3: [ot 9] [ab 5] [pc 6]\n", | ||
"\n", | ||
"After \"ot=7\":\n", | ||
"Box 0: [rn 1] [cm 2]\n", | ||
"Box 3: [ot 7] [ab 5] [pc 6]\n", | ||
"</code></pre>\n", | ||
"<p>All 256 boxes are always present; only the boxes that contain any lenses are shown here. Within each box, lenses are listed from front to back; each lens is shown as its label and focal length in square brackets.</p>\n", | ||
"<p>To confirm that all of the lenses are installed correctly, add up the <em>focusing power</em> of all of the lenses. The focusing power of a single lens is the result of multiplying together:</p>\n", | ||
"<ul>\n", | ||
"<li>One plus the box number of the lens in question.</li>\n", | ||
"<li>The slot number of the lens within the box: <code>1</code> for the first lens, <code>2</code> for the second lens, and so on.</li>\n", | ||
"<li>The focal length of the lens.</li>\n", | ||
"</ul>\n", | ||
"<p>At the end of the above example, the focusing power of each lens is as follows:</p>\n", | ||
"<ul>\n", | ||
"<li><code>rn</code>: <code>1</code> (box 0) * <code>1</code> (first slot) * <code>1</code> (focal length) = <code><em>1</em></code></li>\n", | ||
"<li><code>cm</code>: <code>1</code> (box 0) * <code>2</code> (second slot) * <code>2</code> (focal length) = <code><em>4</em></code></li>\n", | ||
"<li><code>ot</code>: <code>4</code> (box 3) * <code>1</code> (first slot) * <code>7</code> (focal length) = <code><em>28</em></code></li>\n", | ||
"<li><code>ab</code>: <code>4</code> (box 3) * <code>2</code> (second slot) * <code>5</code> (focal length) = <code><em>40</em></code></li>\n", | ||
"<li><code>pc</code>: <code>4</code> (box 3) * <code>3</code> (third slot) * <code>6</code> (focal length) = <code><em>72</em></code></li>\n", | ||
"</ul>\n", | ||
"<p>So, the above example ends up with a total focusing power of <code><em>145</em></code>.</p>\n", | ||
"<p>With the help of an over-enthusiastic reindeer in a hard hat, follow the initialization sequence. <em>What is the focusing power of the resulting lens configuration?</em></p>\n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"HTML(downloaded['part2'])" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.11.6" | ||
}, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |