-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.py
341 lines (315 loc) · 12.8 KB
/
questions.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""Download the questions and inputs for Advent of Code."""
from datetime import date
from os import mkdir, system
from os.path import exists, dirname
from re import sub, search
from threading import Thread
from sys import argv, exit as sys_exit
from bs4 import BeautifulSoup, Tag
from markdownify import markdownify as md
from countdown import countdown
from requests import get, post
AUTH_COOKIE = "53616c7465645f5f4dc25cac8121e993a9108ebde8e5b8007e3d54a90e53493dbca52f126bfcdcf016f2a6f2731dbdcad0f7fc85e2d616ff47e54328d7b022e6"
directory = dirname(__file__)
today = date.today()
most_recent_year = today.year if today.month == 12 else today.year - 1
most_recent_day = today.day if today.month == 12 and today.day <= 25 else 25
def download_day(y: int, d: int):
"""Download the question and input for a given day."""
if not exists(f"{directory}/{y}"):
mkdir(f"{directory}/{y}")
if (
y < 2015
or y > most_recent_year
or (y == most_recent_year and d > most_recent_day)
):
print("That day is not yet available.")
sys_exit(1)
fd = str(d).zfill(2)
print(f"Downloading {y}/{fd}")
with open(f"{directory}/{y}/{fd}.input.txt", "w", encoding="utf-8") as file:
file.write(
get(
f"https://adventofcode.com/{y}/day/{d}/input",
timeout=10,
cookies={"session": AUTH_COOKIE},
).text
)
if not exists(f"{directory}/{y}/{fd}.a.py"):
with open(f"{directory}/{y}/{fd}.a.py", "w", encoding="utf-8") as file:
file.write(
f'with open("{y}/{fd}.input.txt", encoding="utf-8") as file:\n data = file.read()\n\n'
)
if not exists(f"{directory}/{y}/{fd}.b.py"):
with open(f"{directory}/{y}/{fd}.b.py", "w", encoding="utf-8") as file:
file.write(
f'with open("{y}/{fd}.input.txt", encoding="utf-8") as file:\n data = file.read()\n\n'
)
html = get(
f"https://adventofcode.com/{y}/day/{d}",
timeout=10,
cookies={"session": AUTH_COOKIE},
).text
soup = BeautifulSoup(html, "html.parser")
articles = soup.find_all("article")
question = (
"#"
+ sub(
r"\/(\d{4})\/day\/(\d+)",
lambda m: f"../{str(m.group(1))}/{str(int(m.group(2))).zfill(2)}.md",
md(str(articles[0]))[5:].replace(" ---", "\n## Part 1"),
)
+ "\n\n"
+ (
"##"
+ sub(
r"\/(\d{4})\/day\/(\d+)",
lambda m: f"../{str(m.group(1))}/{str(int(m.group(2))).zfill(2)}.md",
md(str(articles[1]))[5:].replace(" ---", ""),
)
if len(articles) > 1
else "Complete for part 2."
)
+ f"\n\nhttps://adventofcode.com/{y}/day/{d}\n\n"
)
with open(f"{directory}/{y}/{fd}.md", "w", encoding="utf-8") as file:
file.write(question)
answer1 = articles[0].find_next_sibling("p").code
if answer1 is not None:
with open(f"{directory}/{y}/{fd}.a.answer.txt", "w", encoding="utf-8") as file:
file.write(answer1.text)
if len(articles) > 1:
answer2 = articles[1].find_next_sibling("p").code
if answer2 is not None:
with open(
f"{directory}/{y}/{fd}.b.answer.txt", "w", encoding="utf-8"
) as file:
file.write(answer2.text)
return question
def download_calendar(y: int):
"""Download the calendar (progress) for a year."""
if 2015 <= y <= most_recent_year:
with open(f"{directory}/{y}/README.md", "w", encoding="utf-8") as file:
req = get(
f"https://adventofcode.com/{y}",
timeout=10,
cookies={"session": AUTH_COOKIE},
)
req.encoding = req.apparent_encoding
html = req.text
soup = BeautifulSoup(html, "html.parser")
calendar = soup.find("pre", {"class": "calendar"})
for span in calendar.find_all("span"):
if span.has_attr("style") and "absolute" in span["style"]:
span.decompose()
if calendar is None:
print("Unknown error.")
print(html)
sys_exit(1)
content = ""
for element in calendar.children:
if isinstance(element, Tag):
if element.name == "a":
lines = element.text.splitlines()
for line in lines[:-1]:
content += f" {line}\n"
if "calendar-complete" in element["class"]:
content += f"* {lines[-1][:-3]}\n"
elif "calendar-verycomplete" in element["class"]:
content += f"* * {lines[-1][:-3]}\n"
else:
content += f" {lines[-1][:-3]}\n"
elif element.name == "span":
content += (
"\n".join(
[" " + line for line in element.text.splitlines()]
)
+ "\n"
)
else:
if element.text.strip() != "":
content += (
"\n".join(
[" " + line for line in element.text.splitlines()]
)
+ "\n"
)
file.write(f"# Advent of Code {y}\n\n```plaintext\n" + content + "```\n")
def download_year(y: int):
"""Download all the questions and inputs for a given year."""
if 2015 <= y <= most_recent_year:
download_calendar(y)
for d in range(1, 26 if y < most_recent_year else (most_recent_day + 1)):
download_day(y, d)
def download():
"""Download all the questions and inputs for Advent of Code."""
for y in range(2015, most_recent_year + 1):
if not exists(f"{directory}/{y}"):
mkdir(f"{directory}/{y}")
Thread(target=download_year, args=(y,)).start()
def download_events():
"""Download the events page."""
with open(f"{directory}/README.md", "w", encoding="utf-8") as file:
html = get(
"https://adventofcode.com/events",
timeout=10,
cookies={"session": AUTH_COOKIE},
).text
soup = BeautifulSoup(html, "html.parser")
events = soup.find_all("div", {"class": "eventlist-event"})
file.write(
"# Advent of Code Events\n\n"
+ "\n".join([f"- [{e.text}]({e.text[1:5]})" for e in events])
+ "\n"
)
def view(y: int, d: int, level: int = 0):
question = download_day(y, d)
system(f"glow {directory}/{y}/{str(d).zfill(2)}.md")
print(f" Input: {directory.replace("\\", "/")}/{y}/{str(d).zfill(2)}.input.txt")
print(
f" Code: {directory.replace("\\", "/")}/{y}/{str(d).zfill(2)}.{("b" if "Part Two" in question else "a") if level == 0 else ("a" if level == 1 else "b")}.py"
)
print()
return question
def accept_answer(y: int, d: int, level: int):
while True:
answer = input("Answer: ")
if answer.strip() == "":
continue
html = post(
f"https://adventofcode.com/{y}/day/{d}/answer",
timeout=10,
cookies={"session": AUTH_COOKIE},
data={"level": level, "answer": answer},
).text
soup = BeautifulSoup(html, "html.parser")
article = soup.find("article")
if article is None:
print("Unknown error.")
print(html)
sys_exit(1)
text = article.text
if "That's the right answer" in text:
print("Correct!")
download_calendar(y)
download_events()
system(f"glow {directory}/{y}/README.md")
if input("Commit (y/N): ").lower() == "y":
download_day(y, d)
system(
f"git add {directory}/{y}/{str(d).zfill(2)}.* {directory}/{y}/README.md {directory}/README.md"
)
system(f'git commit -m "{y}/{str(d).zfill(2)} Part {level}"')
system("git push")
if level == 1:
if "[Continue to Part Two]" in text:
if input("Continue to part two (y/N): ").lower() == "y":
view(y, d, 2)
accept_answer(y, d, 2)
return
elif "That's not the right answer" in text:
print("Incorrect!")
if "too low" in text:
print("Too low.")
elif "too high" in text:
print("Too high.")
if "10 minutes" in text:
print("Wait 10 minutes before trying again.")
countdown(mins=10, secs=0)
elif (m := search(r"((\d+)m )?(\d+)s", text)) is not None:
print(f"Wait {m.group(0)} before trying again.")
countdown(mins=int(m.group(2) or 0), secs=int(m.group(3)))
else:
print("Wait a minute before trying again.")
countdown(mins=1, secs=0)
elif "You gave an answer too recently" in text:
if (m := search(r"((\d+)m )?(\d+)s", text)) is not None:
print(f"Wait {m.group(0)} before trying again.")
countdown(mins=int(m.group(2) or 0), secs=int(m.group(3)))
else:
print("Wait a minute before trying again.")
countdown(mins=1, secs=0)
else:
print("Unknown error.")
print(text)
sys_exit(1)
def main():
download_events()
if len(argv) == 1:
print("DO NOT RUN THIS MORE THAN ONCE!")
download()
elif len(argv) == 2 and argv[1] == "calendars":
for y in range(2015, most_recent_year + 1):
download_calendar(y)
elif len(argv) == 2:
download_year(int(argv[1]))
elif len(argv) == 3 and argv[1] == "today" and argv[2] == "view":
if today.month != 12:
print("Advent of Code is only in December.")
sys_exit(1)
if today.day > 25:
print("Advent of Code is over.")
sys_exit(1)
view(today.year, today.day)
elif len(argv) == 3 and argv[1] == "today" and argv[2] == "answer":
if today.month != 12:
print("Advent of Code is only in December.")
sys_exit(1)
if today.day > 25:
print("Advent of Code is over.")
sys_exit(1)
question = view(today.year, today.day)
if "Part Two" in question:
accept_answer(today.year, today.day, 2)
else:
accept_answer(today.year, today.day, 1)
elif len(argv) == 3 and argv[1] == "next" and argv[2] == "answer":
# find the earliest uncompleted day
for y in range(2015, most_recent_year + 1):
for d in range(1, 26 if y < most_recent_year else (most_recent_day + 1)):
with open(
f"{directory}/{y}/{str(d).zfill(2)}.a.py", encoding="utf-8"
) as file:
if len(file.readlines()) <= 5:
question = view(y, d)
accept_answer(y, d, 1)
if input("Continue (y/N): ").lower() != "y":
sys_exit(0)
with open(
f"{directory}/{y}/{str(d).zfill(2)}.b.py", encoding="utf-8"
) as file:
if len(file.readlines()) <= 5:
question = view(y, d)
accept_answer(y, d, 2)
if input("Continue (y/N): ").lower() != "y":
sys_exit(0)
elif len(argv) == 3:
download_day(int(argv[1]), int(argv[2]))
elif len(argv) == 4 and argv[3] == "view":
y, d = int(argv[1]), int(argv[2])
view(y, d)
elif len(argv) == 4 and argv[3] == "answer":
y, d = int(argv[1]), int(argv[2])
if y > most_recent_year or (y == most_recent_year and d > most_recent_day):
print("That day is not yet available.")
sys_exit(1)
while True:
question = view(y, d)
if "Part Two" in question:
accept_answer(y, d, 2)
else:
accept_answer(y, d, 1)
if y > most_recent_year or (y == most_recent_year and d > most_recent_day):
sys_exit(0)
if input("Continue (y/N): ").lower() == "y":
d += 1
if d == 26:
y += 1
d = 1
continue
break
else:
print("Usage: python questions.py [year] [day]")
sys_exit(1)
if __name__ == "__main__":
main()