-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_problem.py
131 lines (100 loc) · 3.49 KB
/
get_problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import datetime
import os
import sys
from bs4 import BeautifulSoup
import requests
def main():
# If day is passed as command-line argument, get problem for that day,
# otherwise get problem for current day
if len(sys.argv) > 1:
day = sys.argv[1]
else:
# Get the current day
day = datetime.datetime.now().day
# Create directory for day
create_dir(day)
# Get problem and save to file `dayDD/problem.txt`
get_problem(day)
# Create boilerplate file `dayDD/dayDD.py` for solution
make_solution_file(day)
# Ideally, I want to also get the input file and save to `dayDD/input.txt`.
# Haven't figured out how to do that yet though.
# For now, just make an empty file.
make_input_file(day)
# Create directory for the given day
def create_dir(day):
dirpath = f"day{day:0>2}"
if not os.path.exists(dirpath):
os.mkdir(dirpath)
else:
print(f"Directory `{dirpath}` already exists -- not overwriting!")
# Get problem and save to file `dayDD/problem.txt`
def get_problem(day):
# Parse the HTML
response = requests.get(f"https://adventofcode.com/2023/day/{day}")
html = response.text
soup = BeautifulSoup(html, "html.parser")
# Get all elements with the class `day-desc`
# This will get the problem for Part One, and also for Part Two
# if already revealed
elements = soup.find_all(class_="day-desc")
# Get the text inside the elements
problem_text = "\n".join([element.text for element in elements])
# Fix up the formatting a little
problem_text = problem_text.replace(" ---", " ---\n")
problem_text = problem_text.replace("\n", "\n\n")
problem_text = problem_text.replace("\n\n\n\n", "\n\n")
# Write to file
# Note: This will overwrite the existing problem file ONLY if
# the new text is longer than the existing text
file_path = f"day{day:0>2}/problem.txt"
if os.path.exists(file_path):
with open(file_path) as f:
if len(problem_text) >= len(f.read()):
overwrite = True
else:
overwrite = False
else:
overwrite = True
if overwrite:
with open(file_path, "w") as f:
f.write(problem_text)
print(f"Day {day} problem statement written to {file_path}.")
else:
print(
f"File {file_path} already exists with longer text content. Not overwriting."
)
# Make an empty solution file at `dayDD.py`
def make_solution_file(day):
file_path = f"day{day:0>2}/day{day:0>2}.py"
if not os.path.exists(file_path):
with open(file_path, "w") as f:
f.write(
f"""# Advent of Code 2023 - Day {day} solution
def part1():
result = None
print("Part 1:", result)
def part2():
result = None
print("Part 2:", result)
if __name__ == "__main__":
part1()
part2()
"""
)
print(f"Created empty solution file at {file_path}.")
else:
print(f"Solution file already exists at {file_path}. Not overwriting.")
# Make an empty input file at `input/dayDD.txt`
def make_input_file(day):
file_path = input_file_path(day)
if not os.path.exists(file_path):
with open(file_path, "w") as f:
f.write("")
print(f"Created empty input file at {file_path}.")
else:
print(f"Input file already exists at {file_path}. Not overwriting.")
def input_file_path(day):
return f"day{day:0>2}/input.txt"
if __name__ == "__main__":
main()