diff --git a/2023/day_1/puzzle_text.md b/2023/day_1/puzzle_text.md index 102158b..5f202d5 100644 --- a/2023/day_1/puzzle_text.md +++ b/2023/day_1/puzzle_text.md @@ -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 +

--- 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
 pqr3stu8vwx
 a1b2c3d4e5f
 treb7uchet
-```
-In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
+
+

In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.

+

Consider your entire calibration document. What is the sum of all of the calibration values?

-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 +

Your puzzle answer was 56049.

--- 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
 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?
\ No newline at end of file
+
+

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?

diff --git a/2023/day_8/puzzle_text.md b/2023/day_8/puzzle_text.md index d2c29bf..2018e8e 100644 --- a/2023/day_8/puzzle_text.md +++ b/2023/day_8/puzzle_text.md @@ -24,7 +24,7 @@ ZZZ = (ZZZ, ZZZ)

Starting at AAA, follow the left/right instructions. How many steps are required to reach ZZZ?

-

--- Part Two ---

The sandstorm 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 significantly more steps to escape!

+

Your puzzle answer was 12083.

--- Part Two ---

The sandstorm 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 significantly more steps to escape!

What if the map isn't for people - what if the map is for ghosts? Are ghosts even bound by the laws of spacetime? Only one way to find out.

After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z! If you were a ghost, you'd probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z.

For example:

@@ -50,4 +50,4 @@ XXX = (XXX, XXX)
  • Step 6: You choose all of the right paths, leading you to 11Z and 22Z.
  • So, in this example, you end up entirely on nodes that end in Z after 6 steps.

    -

    Simultaneously start on every node that ends with A. How many steps does it take before you're only on nodes that end with Z?

    \ No newline at end of file +

    Simultaneously start on every node that ends with A. How many steps does it take before you're only on nodes that end with Z?

    diff --git a/get_puzzle_text.py b/get_puzzle_text.py index 521cc02..0781897 100644 --- a/get_puzzle_text.py +++ b/get_puzzle_text.py @@ -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('
    ')[1] + #get all the text after the first
    tag + puzzle_text = puzzle_text.split('
    ')[1] + # Remove everything after the puzzle text which is all the things after the
    tag + puzzle_text = puzzle_text.split('
    ')[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() \ No newline at end of file +populate_data()