day 12 done

This commit is contained in:
Jake Pullen
2023-12-12 09:52:36 +00:00
parent c54bc6efe0
commit 4afa5900a0
4 changed files with 1248 additions and 10 deletions
+1000 -1
View File
File diff suppressed because it is too large Load Diff
+84 -4
View File
@@ -1,6 +1,86 @@
import os
with open(r'advent_of_code\2023\day_12\input.txt', 'r') as file:
input = file.read()
input = file.readlines()
print(input)
test_input = '''???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1'''
test_input = test_input.split('\n')
#input = test_input
# Initialize the total number of arrangements
total_arrangements = 0
# Dictionary for memoization
memo = {}
def count_arrangements(springs:str, blocks:list, spring_index:int, block_index:int, current_block_length:int) -> int:
"""
This function calculates the number of valid arrangements of springs and blocks.
Parameters:
springs (str): A string of characters, each of which is either '.', '#' or '?'.
blocks (list): A list of integers representing the blocks.
spring_index (int): The current index in `springs`.
block_index (int): The current index in `blocks`.
current_block_length (int): The current number of consecutive '#' characters.
Returns:
int: The number of valid arrangements for the current state.
"""
# Create a key for the current state
state_key = (spring_index, block_index, current_block_length)
# If the result for the current state is already computed, return it
if state_key in memo:
return memo[state_key]
# If all springs are processed
if spring_index == len(springs):
# If all blocks are used and there's no current block, or the last block is of correct size
if (block_index == len(blocks) and current_block_length == 0) or \
(block_index == len(blocks) - 1 and blocks[block_index] == current_block_length):
return 1
else:
return 0
# Initialize the number of arrangements for the current state
arrangements = 0
# Try both possibilities for the current spring ('.' or '#')
for spring in ['.', '#']:
# If the current spring can be the current possibility
if springs[spring_index] == spring or springs[spring_index] == '?':
# If the current spring is '.' and there's no current block
if spring == '.' and current_block_length == 0:
arrangements += count_arrangements(springs, blocks, spring_index + 1, block_index, 0)
# If the current spring is '.' and the current block is of correct size
elif spring == '.' and current_block_length > 0 and block_index < len(blocks) and blocks[block_index] == current_block_length:
arrangements += count_arrangements(springs, blocks, spring_index + 1, block_index + 1, 0)
# If the current spring is '#'
elif spring == '#':
arrangements += count_arrangements(springs, blocks, spring_index + 1, block_index, current_block_length + 1)
# Store the result for the current state in the memoization dictionary
memo[state_key] = arrangements
# Return the number of arrangements for the current state
return arrangements
# Process each line in the input
for line in input:
# Split the line into springs and blocks
springs, blocks_string = line.split()
# Convert the blocks string into a list of integers
blocks = [int(x) for x in blocks_string.split(',')]
# Clear the memoization dictionary
memo.clear()
# Calculate the number of arrangements for the current line
line_arrangements = count_arrangements(springs, blocks, 0, 0, 0)
# Add the number of arrangements for the current line to the total number of arrangements
total_arrangements += line_arrangements
# Print the total number of arrangements
print(total_arrangements)
+88 -4
View File
@@ -1,6 +1,90 @@
import os
with open(r'advent_of_code\2023\day_12\input.txt', 'r') as file:
input = file.read()
input = file.readlines()
print(input)
test_input = '''???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1'''
test_input = test_input.split('\n')
#input = test_input
# Initialize the total number of arrangements
total_arrangements = 0
# Dictionary for memoization
memo = {}
def count_arrangements(springs:str, blocks:list, spring_index:int, block_index:int, current_block_length:int) -> int:
"""
This function calculates the number of valid arrangements of springs and blocks.
Parameters:
springs (str): A string of characters, each of which is either '.', '#' or '?'.
blocks (list): A list of integers representing the blocks.
spring_index (int): The current index in `springs`.
block_index (int): The current index in `blocks`.
current_block_length (int): The current number of consecutive '#' characters.
Returns:
int: The number of valid arrangements for the current state.
"""
# Create a key for the current state
state_key = (spring_index, block_index, current_block_length)
# If the result for the current state is already computed, return it
if state_key in memo:
return memo[state_key]
# If all springs are processed
if spring_index == len(springs):
# If all blocks are used and there's no current block, or the last block is of correct size
if (block_index == len(blocks) and current_block_length == 0) or \
(block_index == len(blocks) - 1 and blocks[block_index] == current_block_length):
return 1
else:
return 0
# Initialize the number of arrangements for the current state
arrangements = 0
# Try both possibilities for the current spring ('.' or '#')
for spring in ['.', '#']:
# If the current spring can be the current possibility
if springs[spring_index] == spring or springs[spring_index] == '?':
# If the current spring is '.' and there's no current block
if spring == '.' and current_block_length == 0:
arrangements += count_arrangements(springs, blocks, spring_index + 1, block_index, 0)
# If the current spring is '.' and the current block is of correct size
elif spring == '.' and current_block_length > 0 and block_index < len(blocks) and blocks[block_index] == current_block_length:
arrangements += count_arrangements(springs, blocks, spring_index + 1, block_index + 1, 0)
# If the current spring is '#'
elif spring == '#':
arrangements += count_arrangements(springs, blocks, spring_index + 1, block_index, current_block_length + 1)
# Store the result for the current state in the memoization dictionary
memo[state_key] = arrangements
# Return the number of arrangements for the current state
return arrangements
# Process each line in the input
for line in input:
# Split the line into springs and blocks
springs, blocks_string = line.split()
# add in 5 copies of the spring separated by a '?'
springs = '?'.join([springs,springs,springs,springs,springs])
# add in 5 copies of the block_string separated by a ','
blocks_string = ','.join([blocks_string,blocks_string,blocks_string,blocks_string,blocks_string])
# Convert the blocks string into a list of integers
blocks = [int(x) for x in blocks_string.split(',')]
# Clear the memoization dictionary
memo.clear()
# Calculate the number of arrangements for the current line
line_arrangements = count_arrangements(springs, blocks, 0, 0, 0)
# Add the number of arrangements for the current line to the total number of arrangements
total_arrangements += line_arrangements
# Print the total number of arrangements
print(total_arrangements)
+76 -1
View File
@@ -1 +1,76 @@
# Day 12 Puzzle Text.
<article class="day-desc"><h2>--- Day 12: Hot Springs ---</h2><p>You finally reach the hot springs! You can see steam rising from secluded areas attached to the primary, ornate building.</p>
<p>As you turn to enter, the <a href="11">researcher</a> stops you. "Wait - I thought you were looking for the hot springs, weren't you?" You indicate that this definitely looks like hot springs to you.</p>
<p>"Oh, sorry, common mistake! This is actually the <a href="https://en.wikipedia.org/wiki/Onsen" target="_blank">onsen</a>! The hot springs are next door."</p>
<p>You look in the direction the researcher is pointing and suddenly notice the <span title="I love this joke. I'm not sorry.">massive metal helixes</span> towering overhead. "This way!"</p>
<p>It only takes you a few more steps to reach the main gate of the massive fenced-off area containing the springs. You go through the gate and into a small administrative building.</p>
<p>"Hello! What brings you to the hot springs today? Sorry they're not very hot right now; we're having a <em>lava shortage</em> at the moment." You ask about the missing machine parts for Desert Island.</p>
<p>"Oh, all of Gear Island is currently offline! Nothing is being manufactured at the moment, not until we get more lava to heat our forges. And our springs. The springs aren't very springy unless they're hot!"</p>
<p>"Say, could you go up and see why the lava stopped flowing? The springs are too cold for normal operation, but we should be able to find one springy enough to launch <em>you</em> up there!"</p>
<p>There's just one problem - many of the springs have fallen into disrepair, so they're not actually sure which springs would even be <em>safe</em> to use! Worse yet, their <em>condition records of which springs are damaged</em> (your puzzle input) are also damaged! You'll need to help them repair the damaged records.</p>
<p>In the giant field just outside, the springs are arranged into <em>rows</em>. For each row, the condition records show every spring and whether it is <em>operational</em> (<code>.</code>) or <em>damaged</em> (<code>#</code>). This is the part of the condition records that is itself damaged; for some springs, it is simply <em>unknown</em> (<code>?</code>) whether the spring is operational or damaged.</p>
<p>However, the engineer that produced the condition records also duplicated some of this information in a different format! After the list of springs for a given row, the size of each <em>contiguous group of damaged springs</em> is listed in the order those groups appear in the row. This list always accounts for every damaged spring, and each number is the entire size of its contiguous group (that is, groups are always separated by at least one operational spring: <code>####</code> would always be <code>4</code>, never <code>2,2</code>).</p>
<p>So, condition records with no unknown spring conditions might look like this:</p>
<pre><code>#.#.### 1,1,3
.#...#....###. 1,1,3
.#.###.#.###### 1,3,1,6
####.#...#... 4,1,1
#....######..#####. 1,6,5
.###.##....# 3,2,1
</code></pre>
<p>However, the condition records are partially damaged; some of the springs' conditions are actually <em>unknown</em> (<code>?</code>). For example:</p>
<pre><code>???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
</code></pre>
<p>Equipped with this information, it is your job to figure out <em>how many different arrangements</em> of operational and broken springs fit the given criteria in each row.</p>
<p>In the first line (<code>???.### 1,1,3</code>), there is exactly <em>one</em> way separate groups of one, one, and three broken springs (in that order) can appear in that row: the first three unknown springs must be broken, then operational, then broken (<code>#.#</code>), making the whole row <code>#.#.###</code>.</p>
<p>The second line is more interesting: <code>.??..??...?##. 1,1,3</code> could be a total of <em>four</em> different arrangements. The last <code>?</code> must always be broken (to satisfy the final contiguous group of three broken springs), and each <code>??</code> must hide exactly one of the two broken springs. (Neither <code>??</code> could be both broken springs or they would form a single contiguous group of two; if that were true, the numbers afterward would have been <code>2,3</code> instead.) Since each <code>??</code> can either be <code>#.</code> or <code>.#</code>, there are four possible arrangements of springs.</p>
<p>The last line is actually consistent with <em>ten</em> different arrangements! Because the first number is <code>3</code>, the first and second <code>?</code> must both be <code>.</code> (if either were <code>#</code>, the first number would have to be <code>4</code> or higher). However, the remaining run of unknown spring conditions have many different ways they could hold groups of two and one broken springs:</p>
<pre><code>?###???????? 3,2,1
.###.##.#...
.###.##..#..
.###.##...#.
.###.##....#
.###..##.#..
.###..##..#.
.###..##...#
.###...##.#.
.###...##..#
.###....##.#
</code></pre>
<p>In this example, the number of possible arrangements for each row is:</p>
<ul>
<li><code>???.### 1,1,3</code> - <code><em>1</em></code> arrangement</li>
<li><code>.??..??...?##. 1,1,3</code> - <code><em>4</em></code> arrangements</li>
<li><code>?#?#?#?#?#?#?#? 1,3,1,6</code> - <code><em>1</em></code> arrangement</li>
<li><code>????.#...#... 4,1,1</code> - <code><em>1</em></code> arrangement</li>
<li><code>????.######..#####. 1,6,5</code> - <code><em>4</em></code> arrangements</li>
<li><code>?###???????? 3,2,1</code> - <code><em>10</em></code> arrangements</li>
</ul>
<p>Adding all of the possible arrangement counts together produces a total of <code><em>21</em></code> arrangements.</p>
<p>For each row, count all of the different arrangements of operational and broken springs that meet the given criteria. <em>What is the sum of those counts?</em></p>
<p>Your puzzle answer was <code>7307</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>As you look out at the field of springs, you feel like there are way more springs than the condition records list. When you examine the records, you discover that they were actually <em>folded up</em> this whole time!</p>
<p>To <em>unfold the records</em>, on each row, replace the list of spring conditions with five copies of itself (separated by <code>?</code>) and replace the list of contiguous groups of damaged springs with five copies of itself (separated by <code>,</code>).</p>
<p>So, this row:</p>
<pre><code>.# 1</code></pre>
<p>Would become:</p>
<pre><code>.#?.#?.#?.#?.# 1,1,1,1,1</code></pre>
<p>The first line of the above example would become:</p>
<pre><code>???.###????.###????.###????.###????.### 1,1,3,1,1,3,1,1,3,1,1,3,1,1,3</code></pre>
<p>In the above example, after unfolding, the number of possible arrangements for some rows is now much larger:</p>
<ul>
<li><code>???.### 1,1,3</code> - <code><em>1</em></code> arrangement</li>
<li><code>.??..??...?##. 1,1,3</code> - <code><em>16384</em></code> arrangements</li>
<li><code>?#?#?#?#?#?#?#? 1,3,1,6</code> - <code><em>1</em></code> arrangement</li>
<li><code>????.#...#... 4,1,1</code> - <code><em>16</em></code> arrangements</li>
<li><code>????.######..#####. 1,6,5</code> - <code><em>2500</em></code> arrangements</li>
<li><code>?###???????? 3,2,1</code> - <code><em>506250</em></code> arrangements</li>
</ul>
<p>After unfolding, adding all of the possible arrangement counts together produces <code><em>525152</em></code>.</p>
<p>Unfold your condition records; <em>what is the new sum of possible arrangement counts?</em></p>