Day 15 Done

This commit is contained in:
Jake Pullen
2023-12-15 13:49:23 +00:00
parent 2087550892
commit 9892e1dfa6
4 changed files with 281 additions and 10 deletions
File diff suppressed because one or more lines are too long
+72 -4
View File
@@ -1,6 +1,74 @@
import os
with open(r'advent_of_code\2023\day_15\input.txt', 'r') as file:
input = file.read()
input_data = file.read()
print(input)
test_input = 'rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7'
#input_data = test_input
# Function to calculate hash value
def calculate_hash(s):
hash_value = 0
for char in s:
hash_value = ((hash_value + ord(char)) * 17) % 256
return hash_value
# Function to process commands
def process_commands(commands, box):
for command in commands:
if command[-1] == '-':
name = command[:-1]
hash_value = calculate_hash(name)
# Initialize an empty list for the updated box values
updated_box_values = []
# Iterate over each tuple in the box at the hash_value index
for tuple_in_box in box[hash_value]:
# Unpack the tuple into name (n) and value (v)
n, v = tuple_in_box
# If the name does not match the name to be removed, add it to the updated list
if n != name:
updated_box_values.append((n, v))
# Assign the updated list back to the box at the hash_value index
box[hash_value] = updated_box_values
elif command[-2] == '=':
name = command[:-2]
hash_value = calculate_hash(name)
length = int(command[-1])
# Initialize an empty list for the updated box values
updated_box_values = []
# Iterate over each tuple in the box at the hash_value index
for tuple_in_box in box[hash_value]:
# Unpack the tuple into name (n) and value (v)
n, v = tuple_in_box
# Check if the name matches the name in the command
if name == n:
# If it matches, update the value to the new length
updated_value = length
else:
# If it doesn't match, keep the original value
updated_value = v
# Add the tuple with the name and updated value to the list
updated_box_values.append((n, updated_value))
# Assign the updated list back to the box at the hash_value index
box[hash_value] = updated_box_values
# Split the commands
commands = input_data.split(',')
# Calculate the sum of hash values
sum_hash_values = 0
for command in commands:
sum_hash_values += calculate_hash(command)
print(sum_hash_values)
# Initialize the box
box = [[] for _ in range(256)]
# Process the commands
process_commands(commands, box)
+73 -4
View File
@@ -1,6 +1,75 @@
import os
with open(r'advent_of_code\2023\day_15\input.txt', 'r') as file:
input = file.read()
input_data = file.read()
print(input)
test_input = 'rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7'
# input_data = test_input
# Function to calculate hash value
def calculate_hash(string):
hash_value = 0
for char in string:
hash_value = ((hash_value + ord(char)) * 17) % 256
return hash_value
# Function to process commands
def process_commands(commands, box):
# Iterate over each command in the list of commands
for command in commands:
# Check if the last character of the command is '-'
if command[-1] == '-':
# If it is, the command is to remove a name from the box
# Extract the name from the command by removing the last character
name_to_remove = command[:-1]
# Calculate the hash value for the name
hash_value = calculate_hash(name_to_remove)
# Create a new list for the box at the hash value index, excluding the name to remove
box[hash_value] = [(name, value) for (name, value) in box[hash_value] if name != name_to_remove]
elif command[-2] == '=':
# If the second last character of the command is '=', the command is to add or update a name in the box
# Extract the name and the length from the command
name_to_add_or_update = command[:-2]
length = int(command[-1])
# Calculate the hash value for the name
hash_value = calculate_hash(name_to_add_or_update)
# Check if the name is already in the box at the hash value index
if name_to_add_or_update in [name for (name, value) in box[hash_value]]:
# If it is, update the length for the name
box[hash_value] = [(name, length if name == name_to_add_or_update else value) for (name, value) in box[hash_value]]
else:
# If it's not, add the name and length to the box at the hash value index
box[hash_value].append((name_to_add_or_update, length))
# Split the commands
commands = input_data.split(',')
# Calculate the sum of hash values
sum_hash_values = 0
for command in commands:
sum_hash_values += calculate_hash(command)
print(sum_hash_values)
# Initialize the box
box = [[] for _ in range(256)]
# Process the commands
process_commands(commands, box)
# Function to calculate the product of indices and value
def calculate_product(index1, index2, value):
return (index1 + 1) * (index2 + 1) * value
# Initialize the total sum
total_sum = 0
# Iterate over each box with its index
for box_index, box in enumerate(box):
# Iterate over each tuple in the box with its index
for tuple_index, (name, value) in enumerate(box):
# Calculate the product using the function
product = calculate_product(box_index, tuple_index, value)
# Add the product to the total sum
total_sum += product
# Print the total sum
print(total_sum)
+135 -1
View File
@@ -1 +1,135 @@
# Day 15 Puzzle Text.
<article class="day-desc"><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>
<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>
<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>
<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>
<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>
<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>
<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>
<ul>
<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>
<li>Increase the <em>current value</em> by the ASCII code you just determined.</li>
<li>Set the <em>current value</em> to itself multiplied by <code>17</code>.</li>
<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>
</ul>
<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>
<p>So, to find the result of running the HASH algorithm on the string <code>HASH</code>:</p>
<ul>
<li>The <em>current value</em> starts at <code>0</code>.</li>
<li>The first character is <code>H</code>; its ASCII code is <code>72</code>.</li>
<li>The <em>current value</em> increases to <code>72</code>.</li>
<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>1224</code>.</li>
<li>The <em>current value</em> becomes <code><em>200</em></code> (the remainder of <code>1224</code> divided by <code>256</code>).</li>
<li>The next character is <code>A</code>; its ASCII code is <code>65</code>.</li>
<li>The <em>current value</em> increases to <code>265</code>.</li>
<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>4505</code>.</li>
<li>The <em>current value</em> becomes <code><em>153</em></code> (the remainder of <code>4505</code> divided by <code>256</code>).</li>
<li>The next character is <code>S</code>; its ASCII code is <code>83</code>.</li>
<li>The <em>current value</em> increases to <code>236</code>.</li>
<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>4012</code>.</li>
<li>The <em>current value</em> becomes <code><em>172</em></code> (the remainder of <code>4012</code> divided by <code>256</code>).</li>
<li>The next character is <code>H</code>; its ASCII code is <code>72</code>.</li>
<li>The <em>current value</em> increases to <code>244</code>.</li>
<li>The <em>current value</em> is multiplied by <code>17</code> to become <code>4148</code>.</li>
<li>The <em>current value</em> becomes <code><em>52</em></code> (the remainder of <code>4148</code> divided by <code>256</code>).</li>
</ul>
<p>So, the result of running the HASH algorithm on the string <code>HASH</code> is <code><em>52</em></code>.</p>
<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>
<p>For example:</p>
<pre><code>rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7</code></pre>
<p>This initialization sequence specifies 11 individual steps; the result of running the HASH algorithm on each of the steps is as follows:</p>
<ul>
<li><code>rn=1</code> becomes <code><em>30</em></code>.</li>
<li><code>cm-</code> becomes <code><em>253</em></code>.</li>
<li><code>qp=3</code> becomes <code><em>97</em></code>.</li>
<li><code>cm=2</code> becomes <code><em>47</em></code>.</li>
<li><code>qp-</code> becomes <code><em>14</em></code>.</li>
<li><code>pc=4</code> becomes <code><em>180</em></code>.</li>
<li><code>ot=9</code> becomes <code><em>9</em></code>.</li>
<li><code>ab=5</code> becomes <code><em>197</em></code>.</li>
<li><code>pc-</code> becomes <code><em>48</em></code>.</li>
<li><code>pc=6</code> becomes <code><em>214</em></code>.</li>
<li><code>ot=7</code> becomes <code><em>231</em></code>.</li>
</ul>
<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>
<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>
<p>Your puzzle answer was <code>497373</code>.</p><p class="day-success">The first half of this puzzle is complete! It provides one gold star: *</p>
<article class="day-desc"><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>
<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>
<pre><code> +-----+ +-----+ +-----+
Light | Box | | Box | ... | Box |
-----------------------------------------&gt;
| 0 | | 1 | ... | 255 |
+-----+ +-----+ +-----+
</code></pre>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<ul>
<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>
<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>
</ul>
<p>Here is the contents of every box after each step in the example initialization sequence above:</p>
<pre><code>After "rn=1":
Box 0: [rn 1]
After "cm-":
Box 0: [rn 1]
After "qp=3":
Box 0: [rn 1]
Box 1: [qp 3]
After "cm=2":
Box 0: [rn 1] [cm 2]
Box 1: [qp 3]
After "qp-":
Box 0: [rn 1] [cm 2]
After "pc=4":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4]
After "ot=9":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4] [ot 9]
After "ab=5":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4] [ot 9] [ab 5]
After "pc-":
Box 0: [rn 1] [cm 2]
Box 3: [ot 9] [ab 5]
After "pc=6":
Box 0: [rn 1] [cm 2]
Box 3: [ot 9] [ab 5] [pc 6]
After "ot=7":
Box 0: [rn 1] [cm 2]
Box 3: [ot 7] [ab 5] [pc 6]
</code></pre>
<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>
<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>
<ul>
<li>One plus the box number of the lens in question.</li>
<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>
<li>The focal length of the lens.</li>
</ul>
<p>At the end of the above example, the focusing power of each lens is as follows:</p>
<ul>
<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>
<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>
<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>
<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>
<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>
</ul>
<p>So, the above example ends up with a total focusing power of <code><em>145</em></code>.</p>
<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>