-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_calendar.py
More file actions
93 lines (69 loc) · 2.35 KB
/
Copy pathprepare_calendar.py
File metadata and controls
93 lines (69 loc) · 2.35 KB
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
from argparse import ArgumentParser
from pathlib import Path
import requests
from tqdm import tqdm
TEMPLATE = """
import sys
@@@PARSE@@@
def part1():
data = parse_input()
def part2():
data = parse_input()
if __name__ == "__main__":
print(">>> Part 1 <<<")
part1()
print("==============")
print()
print(">>> Part 2 <<<")
part2()
print("==============")
"""
TEMPLATE_PARSE = """
def parse_input():
debug = len(sys.argv) == 2 and sys.argv[1] == "debug"
with open("debug.txt" if debug else "input.txt", "r") as f:
lines = [l.strip() for l in f.readlines() if l.strip()]
return lines
"""
TEMPLATE_PARSE_NUMBER = """
def parse_input():
debug = len(sys.argv) == 2 and sys.argv[1] == "debug"
with open("debug.txt" if debug else "input.txt", "r") as f:
return int(f.read().strip())
"""
TEMPLATE_PARSE_LINE = """
def parse_input():
debug = len(sys.argv) == 2 and sys.argv[1] == "debug"
with open("debug.txt" if debug else "input.txt", "r") as f:
return f.read().strip()
"""
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("year", help="Year of the calendar")
parser.add_argument("cookie", help="Session cookie to download the input")
args = parser.parse_args()
calendar_root = Path(args.year)
calendar_root.mkdir(exist_ok=True)
for day in tqdm(range(1, 26)):
r = requests.get(f"https://adventofcode.com/{args.year}/day/{day}/input", cookies={"session": args.cookie})
if r.status_code == 404:
break
if not r.ok:
r.raise_for_status()
puzzle_input = r.text.strip()
day_folder = calendar_root / f"{day:02}"
day_folder.mkdir(exist_ok=True)
script = day_folder / f"{day:02}.py"
if not script.exists():
with open(script, "w") as f:
parse_template = TEMPLATE_PARSE if "\n" in puzzle_input else TEMPLATE_PARSE_LINE
try:
int(puzzle_input)
parse_template = TEMPLATE_PARSE_NUMBER
except ValueError:
pass
f.write(TEMPLATE.replace("@@@PARSE@@@", parse_template.strip()).lstrip())
input_file = day_folder / "input.txt"
if not input_file.exists():
with open(input_file, "w") as f:
f.write(r.text)