-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·85 lines (61 loc) · 2.26 KB
/
run.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
#!/usr/bin/env python
import collections
import datetime
import importlib
import os
import re
import cachetools
import click
import utils
@cachetools.cached({})
def get_module(year, day):
return importlib.import_module(f'problems_{year}.{day}')
def get_all_available_days(year):
return [
file.removesuffix('.py')
for file in os.listdir(f'problems_{year}')
if re.match(r'\d+\.py', file)
]
def get_parts_by_day(year, problems):
parts_by_day = collections.defaultdict(set)
for problem in problems:
if '.' in problem:
day, part_id = problem.split('.', maxsplit=1)
module = get_module(year, day)
parts = utils.PART_REGISTRY[module.__name__]
part = parts.get(part_id)
if not part:
raise click.ClickException(
f'Part {part_id} of {module} is not registered. Registered parts: {list(parts.keys())}'
)
parts_by_day[int(day)].add(part)
else:
day = problem
module = get_module(year, day)
parts_by_day[int(day)] = set(utils.PART_REGISTRY[module.__name__].values())
return parts_by_day
def execute_day(parts):
for part in sorted(parts, key=lambda part: part.id):
print(f'--- PART {part.id} ---')
part.cmd()
print()
@click.command()
@click.argument('problems', nargs=-1)
@click.option('-y', '--year', nargs=1, type=int, default=datetime.datetime.now().year, show_default=True)
@click.option('-t', '--test', is_flag=True, default=False)
@click.option('-p', '--profile', is_flag=True, default=False)
@click.option('-a', '--all', 'run_all', is_flag=True, default=False)
def cli(problems, year, test, profile, run_all):
utils.IS_TEST = test
utils.IS_TIMED = profile
if run_all:
if problems:
raise click.ClickException('Cannot both specify problems and have -a/-all enabled')
problems = get_all_available_days(year)
parts_by_day = get_parts_by_day(year, problems)
for day, parts in sorted(parts_by_day.items()):
if len(parts_by_day) > 1:
print(f'========== DAY {day} ==========\n')
execute_day(parts)
if __name__ == '__main__':
cli() # pylint: disable=no-value-for-parameter