reworked get_puzzle_text to handle non existant files.

This commit is contained in:
Jake Pullen
2023-12-17 09:59:49 +00:00
parent 2538eb8731
commit 5a5592ad60
2 changed files with 6 additions and 4 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
session_cookie.txt
puzzle_text.mk
puzzle_text.md
input.txt
# Byte-compiled / optimized / DLL files
+5 -3
View File
@@ -27,9 +27,7 @@ def save_puzzle_text(year, day):
os.makedirs(folder, exist_ok=True)
input_file = os.path.join(folder, "puzzle_text.md")
puzzle_text = get_puzzle_text(year, day)
# Remove everything before the puzzle text which is all the things inside the <main> tag
puzzle_text = puzzle_text.split('<main>')[1]
# Remove everything after the puzzle text which is all the things after the </article> tag
puzzle_text = puzzle_text.split('</article>')[0]
print(f'Saving puzzle text to {input_file}')
with open(input_file, "w") as file:
@@ -70,10 +68,14 @@ 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")
# Create the file if it doesn't exist
if not os.path.isfile(input_file):
with open(input_file, "w") as file:
pass
# 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.":
if not existing_puzzle_text: # == f"# Day {day} Puzzle Text.":
return 1
else:
return 2