-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·176 lines (121 loc) · 4.54 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
#!/usr/bin/env python3
from pathlib import Path
from shutil import copy
from scraper import get_available_days, get_available_events, get_daily_progress, get_description, get_input, get_progress, submit_answer
from configargparse import ArgumentParser
import subprocess
def events(args):
events = get_available_events(args)
print("Available events:\n")
print('\n'.join(events))
def progress(args):
progress = get_progress(args)
print("Current progress:\n")
print(progress)
def test(args):
if subprocess.call([f"python3 part{args.part}.py"],
cwd=f"day{args.day:02}",
shell=True) != 0:
return
outf = open(f"day{int(args.day):02}/part{args.part}.out")
answer = outf.readline().strip()
outf.close()
print(answer)
def submit(args):
if subprocess.call([f"python3 part{args.part}.py"],
cwd=f"day{args.day:02}",
shell=True) != 0:
return
outf = open(f"day{int(args.day):02}/part{args.part}.out")
answer = outf.readline().strip()
outf.close()
print(submit_answer(args, answer))
update(args)
def get(args):
p = Path(f"day{int(args.day):02}/")
p.mkdir(exist_ok=True)
if not (p / "README.md").exists():
desc = get_description(args)
with open(str(p / "README.md"), "w") as f:
f.write(desc)
if not (p / "input").exists():
inp = get_input(args)
with open(str(p / "input"), "w") as f:
f.write(inp)
templates = Path(__file__).parent / "templates"
if not (p / "part1.py").exists():
copy(templates / "part1.py", p / "part1.py")
if not (p / "part2.py").exists():
copy(templates / "part2.py", p / "part2.py")
def update(args):
templates = Path(__file__).parent / "templates"
copy(templates / "README.md", "README.md")
f = open("README.md", "r")
fd = f.read()
f.close()
fd = fd.replace("{year}", args.year)
p = get_daily_progress(args)
for i in range(1, 26):
fd = fd.replace(f"{{day{i:02}}}", p.get(i, ''))
f = open("README.md", "w")
f.write(fd)
f.close()
if "day" in args:
get(args)
desc = get_description(args)
with open(f"day{int(args.day):02}/README.md", "w") as f:
f.write(desc)
def init(args):
if args.year is None:
events = list(get_available_events(args))
print("Available events:\n")
print('\n'.join(events))
event = input("\nSelect event: ")
while event.strip() not in events:
event = input("Select event: ")
args.year = event
if args.session is None:
args.session = input("Enter your session cookie: ")
with open(".aocm", "w") as cfg_file:
cfg_file.write(f"year={args.year}\nsession={args.session}\n")
templates = Path(__file__).parent / "templates"
copy(templates / "LICENSE", "LICENSE")
copy(templates / ".gitignore", ".gitignore")
update(args)
for i in range(1, get_available_days(args) + 1):
print(f"\r\x1B[KGetting day {i}!", end="")
args.day = i
get(args)
print(f"\r\x1B[KDone!")
def create_parser():
parser = ArgumentParser(prog="aocm", default_config_files=[".aocm"])
parser.add_argument("--session", help="Session cookie")
parser.add_argument("--year", help="Advent of code year")
parser.set_defaults(func=lambda x: parser.print_help())
subparsers = parser.add_subparsers()
parser_init = subparsers.add_parser("init")
parser_init.set_defaults(func=init)
parser_update = subparsers.add_parser("update")
parser_update.set_defaults(func=update)
parser_update.add_argument("day", type=int, nargs="?")
parser_events = subparsers.add_parser("events")
parser_events.set_defaults(func=events)
parser_progress = subparsers.add_parser("progress")
parser_progress.set_defaults(func=progress)
parser_submit = subparsers.add_parser("submit")
parser_submit.set_defaults(func=submit)
parser_submit.add_argument("day", type=int)
parser_submit.add_argument("part", choices=["1", "2"])
parser_test = subparsers.add_parser("test")
parser_test.set_defaults(func=test)
parser_test.add_argument("day", type=int)
parser_test.add_argument("part", choices=["1", "2"])
parser_get = subparsers.add_parser("get")
parser_get.set_defaults(func=get)
parser_get.add_argument("day", type=int)
return parser
def main():
args = create_parser().parse_args()
args.func(args)
if __name__ == "__main__":
main()