-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.py
90 lines (75 loc) · 2.89 KB
/
aoc.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
import argparse
import importlib
import sys
from pathlib import Path
from registry import registry
def get_input_data(context: dict) -> str:
"""Returns the input file path based on the context and sample flag."""
suffix = "-sample.txt" if context.get("use_sample", False) else "-input.txt"
file = Path(f"inputs/{context['year']}/day{context['day']}{suffix}")
if not file.exists():
print(f"Missing Input Data - {file}")
sys.exit(1)
return file.read_text().strip()
def main() -> None:
"""Entry point for running a specific year's solution."""
if len(sys.argv) < 3:
print("Usage: python aoc.py <year> <day> [<part>] [--sample]")
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument("year", type=int)
parser.add_argument("day", type=int)
parser.add_argument("part", nargs="?", type=int)
parser.add_argument("-s", "--sample", action="store_true")
args = parser.parse_args()
if args.part and args.part not in (1, 2):
print("Invalid Part Specified")
sys.exit(1)
args_day_padded = f"{args.day:02d}"
# Dynamically import the corresponding year module
try:
importlib.import_module(f"years.{args.year}.day{args_day_padded}")
except ModuleNotFoundError:
print(f"No solutions implemented for year {args.year}, day {args.day}")
sys.exit(1)
# Run the specified part or both parts
context = {"year": args.year, "day": args_day_padded}
if args.part:
key = f"year{args.year}_day{args_day_padded}_part{args.part}"
if key in registry:
registry_item = registry[key]
rv = registry_item["func"](
context
| {
"use_sample": args.sample,
"part": args.part,
"completed": registry_item["completed"],
}
)
print(
f"{args.year}-{args_day_padded} // Part {args.part}: {rv} (Completed: {registry_item['completed']})"
)
else:
print(f"No solution registered for {key}")
sys.exit(1)
else:
for part_num in (1, 2):
key = f"year{args.year}_day{args_day_padded}_part{part_num}"
if key in registry:
registry_item = registry[key]
rv = registry_item["func"](
context
| {
"use_sample": args.sample,
"part": part_num,
"completed": registry_item["completed"],
}
)
print(
f"{args.year}-{args_day_padded} // Part {part_num}: {rv} (Completed: {registry_item['completed']})"
)
else:
print(f"No solution registered for {key}")
sys.exit(1)
if __name__ == "__main__":
main()