-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_aoc_day.py
executable file
·91 lines (71 loc) · 2.58 KB
/
start_aoc_day.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
#!/usr/bin/env python3
import fileinput
import os
import re
import sys
from datetime import datetime, timedelta
from dotenv import load_dotenv
curdir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(1, curdir + "/aoc-to-markdown") # xxx: lib does not have __init__
import aoc_to_markdown
# get the same (previous) year for ~11 months after finishing aoc
year = (datetime.now() - timedelta(days=11*30)).year
output_dir = f"{year}/"
def setup_session_id() -> bool:
if os.getenv("SESSION_ID"):
return True
else:
print("SESSION_ID is needed in order to download the input")
answer = input("Want to input the session cookie value? [N/<cookie>] ")
if answer and answer not in "Nn":
os.environ["SESSION_ID"] = answer
return True
return False
def setup_program_arguments(last=False):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(f"{output_dir}/README.md", "w") as f:
f.write(f"# Solutions to [https://adventofcode.com/{year}](https://adventofcode.com/{year})\n\n<Story>\n\n")
for i in range(1,26):
f.write(f"- Day {i:02d}\n")
f.write("\n")
sys.argv = [sys.argv[0], "-o", output_dir, "-y", year]
if last:
folder_syntax = re.compile("^day-(\\d+)$")
last_day = max(
[
int(folder_syntax.search(f).group(1))
for f in os.listdir(output_dir)
if folder_syntax.match(f)
],
default=1,
)
sys.argv.append("-d")
sys.argv.append(f"{last_day}")
elif setup_session_id(): # don't download input again
sys.argv.append("-i")
sys.argv.append("-b")
sys.argv.append(f"solver.py")
def update_readme():
with fileinput.FileInput(f"{output_dir}README.md", inplace=True) as f:
for line in f:
match = re.search("- Day ([0-9]{2})", line.strip())
if match:
day_str = match.group(1)
dirname = f"day-{day_str}"
if os.path.exists(f"{output_dir}{dirname}"):
print(f"- [Day {day_str}]({dirname})")
continue
print(line, end="", flush=True)
def main():
load_dotenv()
# manually set arguments for aoc_to_markdown
# (until main accepts arguments)
if len(sys.argv) <= 1:
setup_program_arguments()
elif len(sys.argv) == 2 and sys.argv[1] == "last":
setup_program_arguments(last=True)
aoc_to_markdown.main()
update_readme()
if __name__ == "__main__":
main()