Last active
December 3, 2022 23:07
-
-
Save onjin/b335d887aa6287f8e470ab3eac9beda5 to your computer and use it in GitHub Desktop.
advent of code executor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Example layout | |
2022/01/input_test.txt | |
2022/01/input_prod.txt | |
2022/01/code.py | |
Example code.py: | |
def phase_1(input: List[str]) -> Any: | |
# lines have '\n' at end | |
return result | |
def phase_2(input: List[str]) -> Any: | |
# lines have '\n' at end | |
return result | |
Example execution: | |
❯ ./advent.py 03 --data test --phase 2 | |
day: 03 | phase: 2 | data: test => 70 | |
❯ ./advent.py 03 --data test --phase 2 --expected 12 | |
day: 03 | phase: 2 | data: test => 70 != expected 12 | |
""" | |
import argparse | |
import datetime | |
import importlib | |
import os | |
class bcolors: | |
HEADER = "\033[95m" | |
OKBLUE = "\033[94m" | |
OKCYAN = "\033[96m" | |
OKGREEN = "\033[92m" | |
WARNING = "\033[93m" | |
FAIL = "\033[91m" | |
ENDC = "\033[0m" | |
BOLD = "\033[1m" | |
UNDERLINE = "\033[4m" | |
def main(args: argparse.Namespace) -> None: | |
# load data | |
data_path = os.path.join(str(args.year), args.day, f"input_{args.data}.txt") | |
with open(data_path, "r") as fp: | |
path = f"{args.year}.{args.day}.code" | |
code = importlib.import_module(path) | |
executor = getattr(code, f"phase_{args.phase}") | |
result = executor(fp.readlines()) | |
return result | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("day") | |
parser.add_argument("--phase", default="1") | |
parser.add_argument("--year", default=datetime.date.today().year) | |
parser.add_argument( | |
"--expected", help="Optional expected result to colorize output" | |
) | |
parser.add_argument( | |
"--data", | |
help="data set; default 'test'; reads day/input_test.txt", | |
default="test", | |
) | |
args = parser.parse_args() | |
result = main(args) | |
output = f"year: {args.year} | day: {args.day} | phase: {args.phase} | data: {args.data} => {result}" | |
if args.expected: | |
if str(result) == str(args.expected): | |
color = bcolors.OKGREEN | |
suffix = f" == expected {args.expected}" | |
else: | |
color = bcolors.FAIL | |
suffix = f" != expected {args.expected}" | |
output = f"{color}{output}{suffix}{bcolors.ENDC}" | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment