-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
477 lines (389 loc) · 12.8 KB
/
main.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import subprocess
import os
import stat
import sys
import re
from datetime import datetime
from time import sleep
try:
from typing import Optional
except:
pass
CONFIG_FILE = ".meta"
SESSION_FILE = ".aoc.secret"
LOG_FILE = ".answers.log"
DEBUG_HTML_FILE = "debug-full-aoc-response.html"
class Config:
def __init__(self) -> None:
self.directory_type = None # type: Optional[str]
self.year = None # type: Optional[int]
self.day = None # type: Optional[int]
self.part = None # type: Optional[int]
self.cookie = ""
shared_config = Config()
def write_meta_file(config=None):
config = config if config is not None else shared_config
with open(CONFIG_FILE, "w") as f:
if config.directory_type is not None:
f.write("dir=>%s\n" % config.directory_type)
if config.year is not None:
f.write("year=>%d\n" % config.year)
if config.day is not None:
f.write("day=>%d\n" % config.day)
if config.part is not None:
f.write("part=>%d\n" % config.part)
def o_int(text):
if text is None:
return None
return int(text)
def find_in_tree(filename):
wd = os.getcwd()
depth = wd.count("/")
search_path = "./"
while not os.path.exists(search_path + filename):
depth -= 1
if depth < 0:
return None
search_path += "../"
return search_path + filename
def write_git_ignore():
lines = [SESSION_FILE, LOG_FILE]
git_path = find_in_tree(".git")
if git_path is None:
return
git_ignore_path = git_path + "ignore"
for line in lines:
try:
with open(git_ignore_path, "r") as f:
read_lines = f.read().splitlines()
read_lines = list(map(lambda l: l.split(" #")[0], read_lines))
read_lines = list(map(lambda l: l.split("#")[0], read_lines))
assert line in read_lines
except:
with open(git_ignore_path, "a") as f:
f.write("\n%s\n" % line)
def read_config():
session_path = find_in_tree(SESSION_FILE)
if session_path is None:
print("No %s file present." % SESSION_FILE)
print("You can log in with `aoc -k <cookie>`")
return False
with open(session_path) as file:
shared_config.cookie = file.read().splitlines()[0]
write_git_ignore()
try:
with open(CONFIG_FILE) as file:
lines = file.read().splitlines()
options = {}
for line in lines:
key, value = line.split("=>")
options[key] = value
shared_config.directory_type = options["dir"]
shared_config.year = int(options["year"])
shared_config.day = o_int(options.get("day", None))
shared_config.part = o_int(options.get("part", None))
return True
except:
print("No %s file present." % CONFIG_FILE)
return False
def curl(endpoint, data="", cookie="", method="POST"):
added_header = (
"-H 'Content-Type: application/x-www-form-urlencoded'"
if method != "GET"
else ""
)
host = endpoint.split("//")[1].split("/")[0]
cmd = """curl '%s' -L \
-X '%s' \
%s \
-H 'Pragma: no-cache' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'Accept-Language: en-US,en;q=0.9' \
-H 'Cache-Control: no-cache' \
-H 'Host: %s' \
-H 'Origin: https://%s' \
-H 'User-Agent: https://github.com/yspreen/aoc' \
-H 'Cookie: %s' \
--data '%s'""" % (
endpoint,
method,
added_header,
host,
host,
cookie,
data,
)
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
shell=True,
)
result = p.communicate()
return result[0].decode()
def which(cmd):
p = subprocess.Popen(
"which %s" % cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
shell=True,
)
result = p.communicate()[0].decode()
return result != ""
def pandoc(from_file, to_file=".tmp.md"):
p = subprocess.Popen(
"pandoc -s -r html %s -o %s" % (from_file, to_file),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
shell=True,
)
_ = p.communicate()[0].decode()
os.remove(from_file)
def log_line(text):
with open(LOG_FILE, "a") as f:
f.write("[%s] %s\n" % (datetime.now().isoformat(), str(text)))
def log(answer, result):
log_line("%s: %s" % (str(answer), str(result)))
def day_directory():
return shared_config.directory_type == "DAY"
def format_seconds(seconds):
if seconds < 60:
return f"{seconds}s"
if seconds < 60 * 60:
return f"{seconds // 60}m {seconds % 60}s"
return f"{seconds // 60 // 60}h {(seconds // 60) % 60}m {seconds % 60}s"
def wait(wait_text, answer):
seconds = 0
for part in wait_text.split(" "):
if part.endswith("s"):
seconds += int(part[:-1])
if part.endswith("m"):
seconds += int(part[:-1]) * 60
if part.endswith("h"):
seconds += int(part[:-1]) * 60
while seconds >= 0:
print(f"\rWaiting {format_seconds(seconds)} before retry... (ctrl+c to stop) ", end='')
sleep(1)
seconds -= 1
print()
submit(answer)
def submit(answer):
if answer == "":
return
if not read_config():
return
if not day_directory():
print("This is not an AOC day directory.")
return
try:
with open(LOG_FILE, "r") as f:
text = f.read()
for line in text.splitlines():
if line.split("] ", 1)[1].startswith(answer + ":"):
return print("We already gave this answer.")
if line.split("] ", 1)[1].startswith(
answer + " (part %d):" % shared_config.part
):
return print("We already gave this answer.")
except Exception:
pass
part = 0 if shared_config.part is None else shared_config.part
if not (1 <= part <= 2):
print("You can only solve two puzzles, one and two.")
return
endpoint = "https://adventofcode.com/%d/day/%d/answer" % (
shared_config.year,
shared_config.day,
)
data = "level=%d&answer=%s" % (shared_config.part, str(answer))
cookie = shared_config.cookie
html = curl(endpoint, data, cookie)
text = parse_answer_html(html)
text.split("(")[0]
correct = False
log_answer = lambda result: log(answer, result)
if " too low. " in text:
print("Too low.")
log_answer("low")
elif " too high. " in text:
print("Too high.")
log_answer("high")
elif "left to wait" in text:
return wait(get_wait_time(text), answer)
elif "not the right answer" in text:
print("Not correct.")
log_answer("wrong")
elif "right answer" in text:
print("Correct! Part %d complete." % shared_config.part)
log_answer("correct")
correct = True
else:
print(
"Whelp. AOC CLI could not parse this server response. Here's what I got:\n"
)
print(text)
print(
"\nMaybe you can help make aoc cli better and report this. I also put the full html response into a temporary file in this directory called %s"
% DEBUG_HTML_FILE
)
print("Might be helpful for debugging! Cheers 🎄")
with open(DEBUG_HTML_FILE, "w") as f:
f.write(html)
return
if not correct or shared_config.part is None:
return
shared_config.part += 1
write_meta_file()
refresh()
def get_wait_time(text):
return text.split(" left")[0].split("have ")[-1]
def parse_answer_html(html):
text = html # type: str
text = text.split("<article>")[1]
text = text.split("</article>")[0]
text = text.replace("\n", " ")
text = re.sub("<.*?>", "", text)
text = re.sub("\\s+", " ", text)
text = text.strip(" ")
return text
def check_requirements():
for cmd in ["curl", "pandoc"]:
if not which(cmd):
print("Please install %s with\n brew install %s" % (cmd, cmd))
return False
return True
def remove_titles(line):
return re.sub("\\[(.*?)\\]\\{title=.*\\}", "\\1", line)
def clean_up_md(file_name):
with open(file_name, "r") as f:
lines = f.read().splitlines()
lines = lines[:-6]
idx = lines.index('::: {role="main"}') + 1
lines = lines[idx:]
lines = list(map(lambda l: l.replace('{target="_blank"}', ""), lines))
lines = list(map(remove_titles, lines))
with open(file_name, "w") as f:
f.write("\n".join(lines) + "\n")
def download_puzzle(config_):
config = config_ # type: Config
download_input(config)
endpoint = "https://adventofcode.com/%d/day/%d" % (config.year, config.day)
with open(".puzzle.tmp.html", "w") as f:
f.write(curl(endpoint, cookie=config.cookie, method="GET"))
pandoc(".puzzle.tmp.html", "%d.md" % config.day)
clean_up_md("%d.md" % config.day)
def refresh():
if not read_config():
return
download_puzzle(shared_config)
download_input(shared_config)
def download_input(config_):
config = config_ # type: Config
endpoint = "https://adventofcode.com/%d/day/%d/input" % (config.year, config.day)
with open("input", "w") as f:
f.write(curl(endpoint, cookie=config.cookie, method="GET"))
def new_day_directory(day_string):
day = int(day_string)
if not read_config():
return
if day_directory():
print("This is not a year directory.")
return
year = shared_config.year
os.makedirs("%02d" % day)
os.chdir("%02d" % day)
try:
config = Config()
config.directory_type = "DAY"
config.year = year
config.day = day
config.part = 1
config.cookie = shared_config.cookie
write_meta_file(config)
download_puzzle(config)
log_line("downloaded")
except Exception as e:
print("Failed to create day directory:", e)
os.chdir("..")
def new_year_directory(year_string):
year = int(year_string)
try:
config = Config()
config.directory_type = "ROOT"
config.year = year
write_meta_file(config)
except:
print("Failed to create year directory.")
def store_key(cookie_):
cookie = cookie_.lower() # type: str
if cookie.startswith("cookie: "):
cookie = cookie[len("cookie: ") :]
if not cookie.startswith("session="):
cookie = "session=" + cookie
with open(SESSION_FILE, "w") as f:
f.write(cookie)
print("(Stored in %s)" % SESSION_FILE)
write_git_ignore()
def install_script():
raw = """
#!/bin/sh
"%s" "%s" "$@"
""" % (
sys.executable,
__file__,
)
raw = raw.strip("\n").splitlines()
raw = list(map(lambda l: l.strip(" \t"), raw))
return "\n".join(raw + [""])
def install():
exec = "/usr/local/bin/aoc"
try:
with open(exec, "w") as f:
f.write(install_script())
os.chmod(exec, os.stat(exec).st_mode | 0o555)
except:
print("Retry with sudo.")
def main():
if not check_requirements():
return False
arg = sys.argv
a_len = len(arg)
if a_len > 1 and arg[-2] in ["-d", "day"]:
return new_day_directory(arg[-1])
if a_len > 1 and arg[-2] in ["-y", "year"]:
return new_year_directory(arg[-1])
if a_len > 1 and arg[-2] in ["-s", "solve"]:
return submit(arg[-1])
if a_len > 1 and arg[-2] in ["-k", "login"]:
return store_key(arg[-1])
if a_len > 0 and arg[-1] in ["-r", "refresh"]:
return refresh()
if a_len > 0 and arg[-1] in ["-i", "install"]:
return install()
print_help()
def print_help():
print("Usage: aoc [command] [options]")
print()
print("Commands:")
print("aoc -d <day> / aoc day <day>: ")
print(" Will create a new folder to solve the day numbered <day>. ")
print()
print("aoc -y <year> / aoc year <year>: ")
print(" Will set up the current folder to contain puzzles for year <day>. ")
print()
print("aoc -s <answer> / aoc solve <answer>: ")
print(" Will attempt to solve the current puzzle with answer <answer>. ")
print()
print("aoc -k <param> / aoc login <param>: ")
print(" Will use your auth cookie to sign you in. Credentials stored (and git ignored automatically) in the current directory. ")
print()
print("aoc -r <param> / aoc refresh <param>: ")
print(" Will reload the puzzle markdown and input. ")
print()
print("aoc -i <param> / aoc install <param>: ")
print(" Will install this script in /usr/local/bin. ")
print(" (requires sudo.) ")
print()
print("For more information, visit https://github.com/yspreen/aoc")
if __name__ == "__main__":
main()