-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.py
98 lines (72 loc) · 3.35 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
91
92
93
94
95
96
97
98
#!/usr/bin/python
import os
import subprocess
import argparse
import sys
import logging
from typing import List
BASE = os.path.dirname(os.path.abspath(__file__))
EVENT_PREFIX = "event"
DAY_PREFIX = "day"
EXECUTABLE = "solution.py"
class EventCommand:
def __init__(self, event, sub_commands=None, parser=None):
self.event = event
self.sub_commands = sub_commands
self.parser = parser
def __str__(self):
return f"(Event: {self.event}, Sub-commands: {self.sub_commands}, Parser: {self.parser})"
def get_event(self):
return self.event
def get_sub_commands(self):
return self.sub_commands
def get_parser(self):
return self.parser
def set_event(self, event):
self.event = event
def set_sub_commands(self, sub_commands):
self.sub_commands = sub_commands
def set_parser(self, parser):
self.parser = parser
def get_commands() -> List[EventCommand]:
events = [x for x in os.listdir(BASE) if os.path.isdir(x) and x.startswith(EVENT_PREFIX)]
opts = []
for event in events:
event_abs = os.path.join(BASE, event)
days = [x for x in os.listdir(event_abs) if os.path.isdir(os.path.join(event_abs, x)) and x.startswith(DAY_PREFIX)]
days.sort()
opts.append(EventCommand(event=event, sub_commands=days))
return opts
def execute_aoc_solution(args):
file_to_execute = os.path.join(BASE, args.event, args.day, EXECUTABLE)
if not os.path.isfile(file_to_execute):
logging.error(f"No such file exists: {file_to_execute}")
raise ValueError(f"No such file exists: {file_to_execute}")
logging.info(f"Executing solution of {args.day} from event {args.event}")
cmd = [sys.executable, file_to_execute]
if args.verbose:
cmd.append("--verbose")
if args.run_sample:
cmd.append("--run-sample")
try:
logging.debug(f"Running command: {' '.join(cmd)}")
subprocess.run(cmd)
except Exception as ex:
logging.error(f"Failed to execute, error occurred: {ex}")
raise ex
logging.info(f"Completed execution of {args.day} from event {args.event}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Run adventofcode problem solutions")
subparsers = parser.add_subparsers(title="event", dest="event", required=True, help='Run problem solutions for adventofcode events')
cmds = get_commands()
for event_cmd in cmds:
event_cmd.set_parser(subparsers.add_parser(event_cmd.get_event(), help=f'Run problem solutions for adventofcode {event_cmd.get_event()}'))
event_cmd.get_parser().add_argument("-d", "--day", choices=event_cmd.get_sub_commands(), help="Problem of the day")
event_cmd.get_parser().add_argument("-v", "--verbose", action="store_true", help="Log with more verbosity")
event_cmd.get_parser().add_argument('-s', '--run-sample', action='store_true', help='Run with sample.txt; if omitted, runs with input.txt')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
else:
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
execute_aoc_solution(args)