updated get puzzle text to be smart

This commit is contained in:
Jake Pullen
2023-12-08 13:42:14 +00:00
parent 43f187ebee
commit 784baab952
3 changed files with 74 additions and 41 deletions
+17 -33
View File
@@ -1,44 +1,28 @@
# Day 1 Puzzle Text.
--- Day 1: Trebuchet?! ---
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
For example:
```
1abc2
<article class="day-desc"><h2>--- Day 1: Trebuchet?! ---</h2><p>Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.</p>
<p>You've been doing this long enough to know that to restore snow operations, you need to check all <em class="star">fifty stars</em> by December 25th.</p>
<p>Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants <em class="star">one star</em>. Good luck!</p>
<p>You try to ask why they can't just use a <a href="/2015/day/1">weather machine</a> ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") <span title="My hope is that this abomination of a run-on sentence somehow conveys the chaos of being hastily loaded into a trebuchet.">and</span> hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a <a href="https://en.wikipedia.org/wiki/Trebuchet" target="_blank">trebuchet</a> ("please hold still, we need to strap you in").</p>
<p>As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been <em>amended</em> by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.</p>
<p>The newly-improved calibration document consists of lines of text; each line originally contained a specific <em>calibration value</em> that the Elves now need to recover. On each line, the calibration value can be found by combining the <em>first digit</em> and the <em>last digit</em> (in that order) to form a single <em>two-digit number</em>.</p>
<p>For example:</p>
<pre><code>1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
```
In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
</code></pre>
<p>In this example, the calibration values of these four lines are <code>12</code>, <code>38</code>, <code>15</code>, and <code>77</code>. Adding these together produces <code><em>142</em></code>.</p>
<p>Consider your entire calibration document. <em>What is the sum of all of the calibration values?</em></p>
Consider your entire calibration document. What is the sum of all of the calibration values?
--- Part Two ---
Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
```
two1nine
<p>Your puzzle answer was <code>56049</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Your calculation isn't quite right. It looks like some of the digits are actually <em>spelled out with letters</em>: <code>one</code>, <code>two</code>, <code>three</code>, <code>four</code>, <code>five</code>, <code>six</code>, <code>seven</code>, <code>eight</code>, and <code>nine</code> <em>also</em> count as valid "digits".</p>
<p>Equipped with this new information, you now need to find the real first and last digit on each line. For example:</p>
<pre><code>two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
```
In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.
What is the sum of all of the calibration values?
</code></pre>
<p>In this example, the calibration values are <code>29</code>, <code>83</code>, <code>13</code>, <code>24</code>, <code>42</code>, <code>14</code>, and <code>76</code>. Adding these together produces <code><em>281</em></code>.</p>
<p><em>What is the sum of all of the calibration values?</em></p>
+2 -2
View File
@@ -24,7 +24,7 @@ ZZZ = (ZZZ, ZZZ)
</code></pre>
<p>Starting at <code>AAA</code>, follow the left/right instructions. <em>How many steps are required to reach <code>ZZZ</code>?</em></p>
<article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The <span title="Duhduhduhduhduh! Dah, duhduhduhduhduh!">sandstorm</span> is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take <em>significantly more steps</em> to escape!</p>
<p>Your puzzle answer was <code>12083</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The <span title="Duhduhduhduhduh! Dah, duhduhduhduhduh!">sandstorm</span> is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take <em>significantly more steps</em> to escape!</p>
<p>What if the map isn't for people - what if the map is for <em>ghosts</em>? Are ghosts even bound by the laws of spacetime? Only one way to find out.</p>
<p>After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in <code>A</code> is equal to the number ending in <code>Z</code>! If you were a ghost, you'd probably just <em>start at every node that ends with <code>A</code></em> and follow all of the paths at the same time until they all simultaneously end up at nodes that end with <code>Z</code>.</p>
<p>For example:</p>
@@ -50,4 +50,4 @@ XXX = (XXX, XXX)
<li>Step 6: You choose all of the <em>right</em> paths, leading you to <code><em>11Z</em></code> and <code><em>22Z</em></code>.</li>
</ul>
<p>So, in this example, you end up entirely on nodes that end in <code>Z</code> after <code><em>6</em></code> steps.</p>
<p>Simultaneously start on every node that ends with <code>A</code>. <em>How many steps does it take before you're only on nodes that end with <code>Z</code>?</em></p>
<p>Simultaneously start on every node that ends with <code>A</code>. <em>How many steps does it take before you're only on nodes that end with <code>Z</code>?</em></p>
+55 -6
View File
@@ -3,8 +3,11 @@ import requests
from datetime import datetime
def get_puzzle_text(year, day):
with open(r'advent_of_code/session_cookie.txt', 'r') as file:
session_cookie = file.read().strip()
url = f"https://adventofcode.com/{year}/day/{day}"
response = requests.get(url)
cookies = {'session': session_cookie}
response = requests.get(url, cookies=cookies)
response.raise_for_status()
return response.text
@@ -15,7 +18,9 @@ def get_puzzle_input(year, day):
cookies = {'session': session_cookie}
response = requests.get(url, cookies=cookies)
response.raise_for_status()
return response.text
# Remove the trailing newline
puzzle_input = response.text.strip()
return puzzle_input
def save_puzzle_text(year, day):
folder = rf"advent_of_code\{year}\day_{day}"
@@ -39,6 +44,39 @@ def save_puzzle_input(year, day):
with open(input_file, "w") as file:
file.write(puzzle_input)
def save_part_2_puzzle_text(year, day):
folder = rf"advent_of_code\{year}\day_{day}"
input_file = os.path.join(folder, "puzzle_text.md")
# grab the existing puzzle text
with open(input_file, "r") as file:
existing_puzzle_text = file.read()
# Check if part 2 has already been added
if "part2" in existing_puzzle_text:
print("Part 2 has already been added. Skipping.")
return
puzzle_text = get_puzzle_text(year, day)
puzzle_text = puzzle_text.split('<main>')[1]
#get all the text after the first </article> tag
puzzle_text = puzzle_text.split('</article>')[1]
# Remove everything after the puzzle text which is all the things after the </article> tag
puzzle_text = puzzle_text.split('</article>')[0]
# Add the existing puzzle text back in
puzzle_text = existing_puzzle_text + puzzle_text
print(f'Saving puzzle text to {input_file}')
with open(input_file, "w") as file:
file.write(puzzle_text)
def get_puzzle_part(year, day):
# Check if the puzzle text for the day equals "# Day {day} Puzzle Text."
folder = rf"advent_of_code\{year}\day_{day}"
input_file = os.path.join(folder, "puzzle_text.md")
# grab the existing puzzle text
with open(input_file, "r") as file:
existing_puzzle_text = file.read()
if existing_puzzle_text == f"# Day {day} Puzzle Text.":
return 1
else:
return 2
# Usage example
current_year = datetime.now().year
@@ -47,7 +85,12 @@ current_day = datetime.now().day
def populate_data(year = current_year, day=current_day):
"""
Populates data for the Advent of Code puzzle.
requires a session cookie to be saved in advent_of_code/session_cookie.txt
Requires a session cookie to be saved in advent_of_code/session_cookie.txt
This function checks if the puzzle text for the day equals "# Day {day} Puzzle Text."
If it does, it saves the puzzle text and input for part 1.
If not, it checks if part 2 has already been added to the puzzle text.
If part 2 has not been added, it saves the puzzle text for part 2.
Args:
year (int): The year of the Advent of Code puzzle. Defaults to the current year.
@@ -56,7 +99,13 @@ def populate_data(year = current_year, day=current_day):
Returns:
None
"""
save_puzzle_text(year, day)
save_puzzle_input(year, day)
part_check = get_puzzle_part(year, day)
if part_check == 1:
save_puzzle_text(year, day)
save_puzzle_input(year, day)
elif part_check == 2:
save_part_2_puzzle_text(year, day)
else:
print("Something went wrong. Check the puzzle text.")
populate_data()
populate_data()