-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_dirs.py
85 lines (65 loc) · 2.03 KB
/
make_dirs.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
import os
try:
# replace your token
AOC_COOKIE = os.environ["AOC_COOKIE"]
except KeyError as e:
print("""please set your https://adventofcode.com/ cookie as AOC_COOKIE first, like this:
export AOC_COOKIE='_ga=GAxxxxxxx; session=yyyyyyyyy; _gid=GAzzzzzzzz'""")
exit(1)
solve_template = """# solve file for day{:02d} problem
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from read_file import by_lines
solve_dir = path.dirname(path.abspath(__file__))
input_path = path.join(solve_dir, "input")
content = by_lines(input_path)
# generated by make_dirs.py
def part1() -> int:
res = 0
return res
def part2() -> int:
res = 0
return res
if __name__ == "__main__":
print("part1", part1())
print("part2", part2())
"""
def make_dirs(maxnum: int):
for i in range(1, maxnum+1):
# create dir
dirname = f"day{i:02}"
if not os.path.isdir(dirname):
os.makedirs(dirname)
else:
print(f"ingored: {dirname} existed")
# make solve file
solvefilename = f"{dirname}/solve.py"
if not os.path.isfile(solvefilename):
with open(solvefilename, "w") as f:
f.write(solve_template.format(i))
else:
print(f"ingored: {solvefilename} existed")
# download input
inputfilename = f"{dirname}/input"
if not os.path.isfile(inputfilename):
content = get_input(i)
with open(inputfilename, "w") as f:
f.write(content)
else:
print(f"ingored: {inputfilename} existed")
print(f"======done for problem {i:02}")
def get_input(i: int) -> str:
import httpx
url = f"https://adventofcode.com/2022/day/{i}/input"
headers = {
"cookie": AOC_COOKIE
}
r = httpx.get(url, headers=headers)
return r.text
if __name__ == "__main__":
from sys import argv
if len(argv) < 2:
print("please input number of issue")
else:
make_dirs(int(argv[1]))